合并两个或多个 json 个文件

Joint two or more json files

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

但是我遇到一个错误:

这是第 44 行错误:

希望你能帮助我,或者你有更好的代码或登录...就像使用 foreach...

我还想让 link 由数字自动生成,例如 ?page=1、?page=2 等等...

这是我的 json: json1

和json2:

如果你的两个数组结构相同在这里你将 $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);

你可以这样做:

$user = array();
$user[] = json_decode($json1,true);
$user[] = json_decode($json2,true);
$json_merge = json_encode($user);
$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);

我已经创建了两个小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"
        }
    }
    ]
}