繁体   English   中英

在PHP中合并两个JSON数组

[英]Merge two JSON array in PHP

我需要在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;

我递归地向“test3.json”文件添加了我在“my-file.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)))方法json_encode(array_merge(json_decode($first_json, true),json_decode($second_json, true)))它给了我: 代码+结果

我做错了什么?

您首先需要将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 

这是你想要的:

<?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

它给了我这个: 代码+结果

这都是因为你做了两个数组的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)))

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


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

<?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);

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

你会给出预期的结果:

[{"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"}]

只是这样做。 您可以添加自己的错误处理,但基本应该是:

$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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM