I have a problem need to join or merge two or more json file..
so far here's my code:
//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);
hopefully you can help me or do you have a much better code or login for this one.. like using foreach...
I also want to make the link auto generated by number like ?page=1, ?page=2 and so on...
I have create two small json
for join it ! Check below,
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"
}
}
]
}
Now i join that using 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 encoded Output
{
"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"
}
}
]
}