2017-07-03 48 views
1

我有一個問題需要加入或合併兩個或兩個以上的JSON文件..聯合兩個或兩個以上的JSON文件

迄今爲止這裏是我的代碼:

//first 
    $url1="https://www.zopim.com/api/v2/chats"; 
    $ch1 = curl_init(); 
    curl_setopt($ch1, CURLOPT_URL, $url1); 
    curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password"); 
    curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); 
    $output1 = curl_exec($ch1); 
    $info1 = curl_getinfo($ch1); 
    curl_close($ch1); 

    $chats1 = json_decode($output1,true); 

    //second 
    $url2="https://www.zopim.com/api/v2/chats?page=2"; 
    $ch2 = curl_init(); 
    curl_setopt($ch2, CURLOPT_URL, $url2); 
    curl_setopt($ch2, CURLOPT_USERPWD, "$username:$password"); 
    curl_setopt($ch2, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); 
    $output2 = curl_exec($ch2); 
    $info2 = curl_getinfo($ch2); 
    curl_close($ch2); 

    $chats2 = json_decode($output2,true); 



    $r = []; 
    if(is_array($chats1) && is_array($chats2)) 
    { 
     foreach($chats1 as $key => $array) 
     { 
      $r[$key] = array_merge($chats2[$key], $array); 
     } 
    } 
    else 
    { 
    echo 'problem with json'; 
    } 
    echo json_encode($r, JSON_UNESCAPED_SLASHES); 

,但我遇到了一個錯誤: enter image description here

這裏的44號錯誤行: enter image description here

希望你可以幫我或你有一個更好的合作德或登錄本一個..喜歡使用的foreach ...

我也想使由數生成的鏈接自動樣?頁= 1?頁= 2,依此類推......

這裏的我的JSON: json1 enter image description here

和json2: enter image description here

+0

請提供你的'array'。 –

+0

等..我會發布它... – ivor

+0

@JaydeepMor isend截圖..JSON文件太長... – ivor

回答

0

我已創建兩個小json爲加入吧!檢查下面,

JSON - 1

{ 
    "chats": [ 
     { 
      "comment": null, 
      "triggered_response": true, 
      "visitor": { 
       "phone": "" 
      }, 
      "session": { 
       "city": "Moncton", 
       "end_date": "2017-07-03", 
       "ip": "99.240.22.84" 
      }, 
      "duration": "32", 
      "agent_names": { 
       "name": "test" 
      } 
     } 
    ] 
} 

JSON - 2

{ 
    "chats": [ 
     { 
      "comment": null, 
      "triggered_response": true, 
      "visitor": { 
       "phone": "" 
      }, 
      "session": { 
       "city": "Moncton1", 
       "end_date": "2017-08-03", 
       "ip": "99.240.22.85" 
      }, 
      "duration": "321", 
      "agent_names": { 
       "name": "test1" 
      } 
     } 
    ] 
} 

現在我加入,使用array_merge_recursive

PHP

// $json is first json encoded string same as $json1 is second encoded string. 
$merge_array = array_merge_recursive(json_decode($json,true),json_decode($json1,true)); 

JSON編碼輸出

{ 
    "chats": [ 
    { 
     "comment": null, 
     "triggered_response": true, 
     "visitor": { 
      "phone": "" 
     }, 
     "session": { 
      "city": "Moncton", 
      "end_date": "2017-07-03", 
      "ip": "99.240.22.84" 
     }, 
     "duration": "32", 
     "agent_names": { 
      "name": "test" 
     } 
    }, 
    { 
     "comment": null, 
     "triggered_response": true, 
     "visitor": { 
      "phone": "" 
     }, 
     "session": { 
      "city": "Moncton1", 
      "end_date": "2017-08-03", 
      "ip": "99.240.22.85" 
     }, 
     "duration": "321", 
     "agent_names": { 
      "name": "test1" 
     } 
    } 
    ] 
} 
+0

如何使用uising foreach生成json文件名的編號... – ivor

+0

爲什麼你要'foreach'。 –

+0

該網址是自動生成的...它增加像?page = 2等... – ivor

0

如果兩個陣列結構同樣在這裏,您通過$ chats2 [$關鍵]作爲array_merge字符串();所以發生錯誤

$res_arr = array_merge($chats2,$chats1); 
array_values($res_arr); 

$r = []; 
if(!empty($res_arr)) 
{ 
    foreach($res_arr as $key => $array) 
    { 
     $r[$key] = $array; 
    } 
} 
else 
{ 
    echo 'problem with json'; 
} 
echo json_encode($r); 
+0

如何在foreach上使用它?... – ivor

+0

請嘗試我更新的答案,並讓我知道如果有任何問題 –

0

你可以這樣做:

$user = array(); 
$user[] = json_decode($json1,true); 
$user[] = json_decode($json2,true); 
$json_merge = json_encode($user); 
0
$finalArray = []; 
$finalArray[] = json_decode($json1,true); 
$finalArray[] = json_decode($json2,true); 
$mergedJSON = json_encode($finalArray); 

對於同一陣列結構,你可以使用 array_merge($array1, $array2) 方法&然後使用 json_encode()

例子:

$mergedArray = array_merge($array1, $array2); 
$mergedJSON = json_encode($mergedArray);