[英]Merge two JSON array in PHP
I need to merge two json file in PHP . 我需要在PHP中合并两个json文件。 One is at first empty, and the other one changes at each call.
一个是空的,另一个是每次通话都会改变。
I found a lot of codes to merge two JSON array into one in PHP but it doesn't work for me. 我发现很多代码在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;
I add recursively to the "test3.json" file the data that I find in the "my-file.json". 我递归地向“test3.json”文件添加了我在“my-file.json”中找到的数据。
This should give me : 这应该给我:
[{"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"}]
As it gives me: 因为它给了我:
[[[{"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"}]
I also tried the method json_encode(array_merge(json_decode($first_json, true),json_decode($second_json, true)))
It gives me this : Code + result 我也尝试了
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)))
它给了我: 代码+结果
What did I do wrong? 我做错了什么?
You first need to convert your JSON
to arrays before you can do anything with it in PHP
. 您首先需要将
JSON
转换为数组,然后才能在PHP
执行任何操作。
First you need to create a new empty array in PHP
and add the JSON
arrays to that empty array. 首先,您需要在
PHP
创建一个新的空数组,并将JSON
数组添加到该空数组中。
Then encode the array again to JSON
and write it to a file. 然后再将数组编码为
JSON
并将其写入文件。
Your full code: 您的完整代码:
$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
Here is what you want: 这是你想要的:
<?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 http://sandbox.onlinephpfunctions.com/code/404972fb59b4f7323f81ee444a1cb14f772f7748
It gives me this : Code + result
它给了我这个: 代码+结果
It's all because of you do array_merge
of two arrays and second one will rewrite the first values. 这都是因为你做了两个数组的
array_merge
而第二个将重写第一个值。 You should add second array to the result array, not 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)))
json_encode(array_merge(json_decode($ first_json,true),json_decode($ second_json,true)))
This will not work, because in each file you have encoded object , not array of objects. 这不起作用,因为在每个文件中您都有编码对象 ,而不是对象数组。 If you have in first file
{"a":1}
and in second file {"a":2}
, when you'd merge their decoded values the result will be {"a":2}
, but if you'd edit them to [{"a": 1}]
and [{"a": 2}]
, the the result will be [{"a": 1}, {"a": 2}]
because now you to merge the array of objects converted to array, not objects. 如果你有第一个文件
{"a":1}
和第二个文件{"a":2}
,当你合并他们的解码值时,结果将是{"a":2}
,但如果你有将它们编辑为[{"a": 1}]
和[{"a": 2}]
,结果将是[{"a": 1}, {"a": 2}]
因为现在要合并转换为数组的对象数组,而不是对象。
If you want to add the data from both files recursively, you should do something like this: 如果要以递归方式从两个文件中添加数据,则应执行以下操作:
<?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 http://sandbox.onlinephpfunctions.com/code/f4858bcc87da34e4ee6639b2ee96c7ac3244ae1b
And you will give expected result: 你会给出预期的结果:
[{"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"}]
Just simply do this. 只是这样做。 You can add you own Error handling but the basic should be like :
您可以添加自己的错误处理,但基本应该是:
$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.