[英]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.