在PHP中合并两个JSON数组

时间:2019-06-11 22:24:48

标签: php json merge

我需要在PHP中合并两个 json文件。一个首先是空的,另一个在每次调用时都会更改。

我发现很多代码可以在PHP中将两个JSON数组合并为一个,但这对我不起作用。

$final_array = array();
$first_json = file_get_contents("test3.json");
$second_json = file_get_contents("my-file.json");


if(json_decode($first_json,true) == null){
    $final_array[] = json_decode($second_json,true);
    $merge_final_array = json_encode(json_decode($second_json,true));
}else{
    $final_array[] = json_decode($first_json,true);
    $final_array[] = json_decode($second_json,true);
    $merge_final_array = json_encode($final_array);
}

file_put_contents("test3.json", $merge_final_array);
$merge_final_array = null;

我将在“ my-file.json”中找到的数据递归添加到“ test3.json”文件中。

这应该给我:

[{"score":15,"win":true,"device":"Android SDK built for x86"},{"score":"Finish","device":"Android SDK built for x86","win":true},{"win":true,"score":"Finish","device":"Android SDK built for x86"},{"score":16,"scenario":"Finish","win":true,"device":"Android SDK built for x86"}]

它给了我:

[[[{"score":15,"win":true,"device":"Android SDK built for x86"},{"score":"Finish","device":"Android SDK built for x86","win":true}],{"win":true,"score":"Finish","device":"Android SDK built for x86"}],{"score":16,"scenario":"Finish","win":true,"device":"Android SDK built for x86"}]

我也尝试了方法json_encode(array_merge(json_decode($first_json, true),json_decode($second_json, true))) 它给了我这个: Code + result

我做错了什么?

3 个答案:

答案 0 :(得分:1)

您首先需要将JSON转换为数组,然后才能在PHP中对其进行任何操作。

首先,您需要在PHP中创建一个新的空数组,并将JSON数组添加到该空数组中。
然后将数组再次编码为JSON并将其写入文件。
您的完整代码:

$first_json = file_get_contents("test3.json");
$second_json = file_get_contents("my-file.json");

$first_array = json_decode($first_json, true); // make array of json
$second_array = json_decode($second_json, true); // make array of json

$final_array = array(); // create an empty array 
$final_array[] = $first_array; // add first array to empty array
$final_array[] = $second_array;  // add second array to array

$final_json = json_encode($final_array); // make a json from the array again

file_put_contents("test3.json", $final_json); // put the json inside a new file 

答案 1 :(得分:0)

这是您想要的:

<?php

$finalArray = [];

// file_get_contents("test3.json");
$firstFileContent = '{"score":15,"win":true,"device":"Android SDK built for x86"}';

// file_get_contents("my-file.json")
$secondFileContent = '{"score":"Finish","device":"Android SDK built for x86","win":true}';

$firstJson = json_decode($firstFileContent, true);
if(JSON_ERROR_NONE === json_last_error()){
    $finalArray[] = $firstJson;
}

$finalArray[] = json_decode($secondFileContent, true);

$finalJson = json_encode($finalArray);

http://sandbox.onlinephpfunctions.com/code/404972fb59b4f7323f81ee444a1cb14f772f7748

  

它给了我这个:Code + result

这是因为您对两个数组进行了array_merge,第二个数组将重写第一个值。您应该添加第二个数组到结果数组,而不是合并。

array_merge(["a" => 1], ["a" => 2]) // result: ["a" => 2]

$result[] = ["a" => 1];
$result[] = ["a" => 2];
// result: [["a" => 1], ["a" => 2]]
  

json_encode(array_merge(json_decode($ first_json,true),json_decode($ second_json,true)))

这将不起作用,因为您在每个文件中都编码了 object ,而不是对象数组。如果在第一个文件{"a":1}和第二个文件{"a":2}中都有,则合并它们的解码值时,结果将是{"a":2},但是如果将它们编辑为{{1 }}和[{"a": 1}],结果将为[{"a": 2}],因为现在您要合并转换为数组而不是对象的对象数组。


如果要递归添加两个文件中的数据,则应执行以下操作:

[{"a": 1}, {"a": 2}]

http://sandbox.onlinephpfunctions.com/code/f4858bcc87da34e4ee6639b2ee96c7ac3244ae1b

您将给出预期的结果:

<?php

$finalArray = [];

$firstFileContent = '[[{"score":15,"win":true,"device":"Android SDK built for x86"},{"score":"Finish","device":"Android SDK built for x86","win":true}],{"win":true,"score":"Finish","device":"Android SDK built for x86"}]';
$secondFileContent = '{"score":16,"scenario":"Finish","win":true,"device":"Android SDK built for x86"}';

mergeJsonRecursively($finalArray, $firstFileContent);
mergeJsonRecursively($finalArray, $secondFileContent);

function mergeJsonRecursively(array &$result, string $json)
{
    $decodedJson = json_decode($json);
    if (JSON_ERROR_NONE === json_last_error()) {
        tryToAddElementToResult($result, $decodedJson);

        if (is_array($decodedJson)) {
            foreach ($decodedJson as $element) {
                tryToAddElementToResult($result, $element);

                if (is_array($element)) {
                    mergeJsonRecursively($result, json_encode($element));
                }
            }
        }
    }
}

function tryToAddElementToResult(array &$result, $element)
{
    if (is_object($element)) {
        $result[] = json_decode(json_encode($element), true);
    }
}

$finalJson = json_encode($finalArray);

答案 2 :(得分:-1)

只需执行此操作即可。您可以添加自己的错误处理,但基本操作应类似于:

$final_array = array();
$first_json = file_get_contents("test3.json");
$second_json = file_get_contents("my-file.json");

$first_array = json_decode($first_json,true);
$second_array = json_decode($second_json,true);

if(is_array($first_array) and is_array(second_array)){
    $final_array = array_merge(first_array,second_array);
}
else if(!empty($first_array) and is_array($first_array)){
    $final_array = $first_array;
}
else if(!empty(second_array) and is_array(second_array)){
    $final_array = second_array;
}

$final_json = json_encode(final_array);

file_put_contents("test3.json", final_json);