最初,我的腳本根據放置在URL中的某些參數來獲取和解碼JSON,然後將這些值插入到數組中並重新編碼爲JSON。我必須提出兩個獨立的請求,一個用於facebook和一個兩個instagram,併合並輸出數據。我的問題是兩個數組似乎並不合併,只輸出array_merge函數中的最後一個數據集。合併兩個數據集PHP
$igjsonData = json_decode((file_get_contents($igurl)));
$fbjsonData = json_decode((file_get_contents($fburl)));
$igresponse = array();
$igresponse["data"] = array();
foreach ($igjsonData->data as $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 as $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;
我需要或者合併後的數據已被重新編碼爲JSON,或重新編碼之前將它們合併爲數組,我希望能夠在順序排列的數據創造時間,如果有人知道如何做到這一點。不太確定這裏的問題實際上是什麼。
我已經部分地得到它的工作,而不是使用'數組'功能,但是仍然想知道如何在我的新數組中創建時間組織。
$output["data"] = array_merge($igresponse, $fbresponse);
$array = $output["data"];
echo json_encode($array);