arrays - Merge Two Data Sets PHP -


initially script fetching , decoding json according parameters placed in url, these values inserted array , re-encoded json. have make 2 separate requests, 1 facebook , 1 2 instagram, , merge output data. problem 2 arrays don't seem merge , output last data set in array_merge function.

$igjsondata = json_decode((file_get_contents($igurl))); $fbjsondata = json_decode((file_get_contents($fburl)));  $igresponse = array(); $igresponse["data"] = array(); foreach ($igjsondata->data $igkey=>$igvalue) {                      $igdata = array();         $igdata["network"] = "instagramicon.png";         $igdata["createdtime"] = $igvalue->caption->created_time;         $igdata["username"] = $igvalue->caption->from->username;         $igdata["profileimage"] = $igvalue->caption->from->profile_picture;         $igdata["caption"] = $igvalue->caption->text;         $igdata["postimage"] = $igvalue->images->standard_resolution->url;          array_push($igresponse["data"], $igdata); }  $fbresponse = array(); $fbresponse["data"] = array(); foreach ($fbjsondata->data $fbkey=>$fbvalue) {                      $fbdata = array();         $fbdata["createdtime"] = $fbvalue->created_time;         $fbdata["username"] = $fbvalue->from->name;                 $data["profileimage"] = "profile_picture";         $fbdata["caption"] = $fbvalue->message;         $data["postimage"] = "postimage";          array_push($fbresponse["data"], $fbdata); }  $output = array_merge($igresponse, $fbresponse); echo $output; 

i need either merge data after has been re-encoded json, or merge them arrays, before re-encode, i'd able arrange data in order of createdtime, if know's how too. not sure problem here is.

i've partially got working, using the 'array' function instead, still know how organise createdtime in new array.

$output["data"] = array_merge($igresponse, $fbresponse); $array = $output["data"]; echo json_encode($array);  

you using same keys both arrays , mentioned on site http://php.net/manual/en/function.array-merge.php, newest array replace previous 1 then.

the solution probally change key "data" sencond array if want instagram data in different element. or can use array_merge_recursive(). if want facebook data , instagram data in same element have rename keys in second array.

i used example exprimenting: http://phpfiddle.org/main/code/bkc-t76


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -