联合两个或多个json文件

时间:2017-07-03 04:52:31

标签: php json api foreach array-merge

我有一个问题需要加入或合并两个或多个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 ...

我还想让链接自动生成的数字如?page = 1,?page = 2等等......

这是我的json: json1 enter image description here

和json2: enter image description here

4 个答案:

答案 0 :(得分:0)

如果你的两个数组结构相同这里你将$ chats2 [$ key]作为字符串传递给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);

答案 1 :(得分:0)

你可以这样做:

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

答案 2 :(得分: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);

答案 3 :(得分: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"
        }
    }
    ]
}