如何在PHP中合并两个JSON字符串? [英] How can I merge two JSON strings in PHP?
本文介绍了如何在PHP中合并两个JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个像这样的JSON字符串:
I have two JSON strings like these:
$json1 = '[ {"src":"1","order":"2"}, {"src":"10","order":"20"}, ... ]';
和
$json2 = '[ {"src":"4","order":"5"}, {"src":"6","order":"7"}, ... ]';
我正在尝试使用它来合并它们:
I am trying to use this to merge them:
$images = array_merge(json_decode($json1 ),json_decode($json2));
$json = '[';
$comma = null;
foreach($images as $image)
{
$comma = ',';
$json .= $comma.'{"src":"'.$image['src'].'","order":"'.$image['order'].'"}';
}
$json .= ']';
echo $json;
但我收到此错误:
错误:无法使用stdCLASS的对象类型.
ERROR: can not use object type of stdCLASS..
我在做什么错了?
推荐答案
调用json_decode时,会将其解码为对象.如果要使其成为数组,则必须这样做
When you're calling json_decode, you're decoding it as an object. If you want it to be an array, you have to do
$images = array_merge(json_decode($json1, true), json_decode($json2, true));
有关json_decode的更多信息:
http://php.net/manual/en/function.json-decode. php
For more information about json_decode:
http://php.net/manual/en/function.json-decode.php
这篇关于如何在PHP中合并两个JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文