日期:2014-05-17  浏览次数:20541 次

如何合并2个JSON数组
C# code
$json1 = <<<ETE
[
{"a":"ab"},
{"a":"da"},
{"a":"gf"},
{"a":"ta"},
{"a":"lc"},
{"a":"ea"},
{"a":"ij"},
{"a":"vb"},
{"a":"lp"},
{"a":"uw"},
{"a":"il"}
]
ETE;
$json2 = <<<ETE
[
{"a":"aa"},
{"a":"ca"},
{"a":"ga"},
{"a":"ha"},
{"a":"la"},
{"a":"oa"},
{"a":"aj"},
{"a":"vr"},
{"a":"pl"},
{"a":"di"},
{"a":"im"}
]
ETE;


如何合并2个JSON数组?(没有decode的情况下)
array_merge 对JSON无效。有没有什么简易的函数,或者FUNCTION?



------解决方案--------------------
PHP code

$arr1 = array();
$arr2 = array();
$arr3 = array();

$web=json_decode($json1);
$arr1=json_to_array($web);
$web=json_decode($json2);
$arr2=json_to_array($web);

$arr3 = array_merge($arr1,$arr2);
echo "<pre>";
print_r($arr3);

function json_to_array($web){
$arr=array();
foreach($web as $k=>$w){
if(is_object($w)) $arr[$k]=json_to_array($w); 
else $arr[$k]=$w;
}
return $arr;
}