arrays - Cannot merge two JSON objects in PHP, the result is null -
i want add json object other json object in php, tried , many other methods, cannot find correct method
this have:
$js_string1 = "{\"info\":[{\"thumb\":\"\",\"count\":1,\"date\":\"11/11/2016 4:05:28\",\"categories\":[null,null,null,null,null],\"sharing\":\"\",\"status\":\"private\",\"title\":\"apple\",\"windows\":[{\"alwaysontop\":false,\"focused\":true,\"width\":1440,\"windowid\":825},{\"active\":false,\"audible\":false, \"height\":727,\"width\":1440,\"windowid\":825}],\"top\":26,\"type\":\"normal\",\"width\":1440}]}"; $js_string2 = "{\"thumb\":\"\",\"count\":1,\"date\":\"10/10/2010 5:07:30\",\"categories\":[null,null,null,null,null],\"sharing\":\"\",\"status\":\"private\",\"title\":\"some title\",\"windows\":[{\"alwaysontop\":false,\"focused\":true,\"width\":1024,\"windowid\":201},{\"active\":false,\"audible\":false, \"height\":500,\"width\":1024,\"windowid\":301}],\"top\":26,\"type\":\"normal\",\"width\":1024}"; $result = json_encode(array_merge(json_decode($js_string1, true),json_decode($js_string2, true)));
expected result is:
{"info":[{"thumb":"","count":1,"date":"11/11/2016 4:05:28","categories":[null,null,null,null,null],"sharing":"","status":"private","title":"apple","windows":[{"alwaysontop":false,"focused":true,"width":1440,"windowid":825},{"active":false,"audible":false, "height":727,"width":1440,"windowid":825}],"top":26,"type":"normal","width":1440}] }, {"thumb":"","count":1,"date":"10/10/2010 5:07:30","categories":[null,null,null,null,null],"sharing":"","status":"private","title":"some title","windows":[{"alwaysontop":false,"focused":true,"width":1024,"windowid":201},{"active":false,"audible":false, "height":500,"width":1024,"windowid":301}],"top":26,"type":"normal","width":1024}]} ]}
may explain , show me how correctly? because tried many different ways , cannot find how correctly.
what want add $js_string2 $js_string1 , keep same structure $js_string1, like:
{"info":[ { .... }, { $js_string2 } ]}
your code fine, both json strings not. both have additional "]}" @ end.
executing code raises warning array_merge(): argument #1 not array
. should have let cause.
edit
array_merge
creates new array containing (in case) keys of first , second array. these keys "info"
, "thumb"
. result (again json) {"info": ..., "thumb": ...}
.
what want add second array info-array of first one, i.e. following.
$result = json_decode($js_string1, true); $result["info"][] = json_decode($js_string2, true);
Comments
Post a Comment