详细说明
You can join the JSON array based on the key value that you obtain provided you have to give under which key you have to join the
json_array()
.
我将考虑json_objects
根据PHP代码如下。
<?php
$array1 = '[
{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000"},
{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0"},
{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0"}
]';
$array2 = '[
{"PlayerID":"17794204","Trouble":"2"},
{"PlayerID":"21532584","Trouble":"0"},
{"PlayerID":"21539896","Trouble":"0"}
]';
?>
因此,为了合并 json 对象,我们必须首先使用json_decode()
对于我们获得的两个数组。
$decode_one = json_decode($array1,TRUE);
$decode_two = json_decode($array2,TRUE);
因此输出为json_decoded()
字符串将如下所示。
第一个解码字符串:
Array ( [0] => Array ( [PlayerID] => 17794204 [userName] => Vandiel [castleCount] => 9 [NotUpd] => 1476253231000 ) [1] => Array ( [PlayerID] => 21532584 [userName] => Mayland [castleCount] => 1 [NotUpd] => 0 ) [2] => Array ( [PlayerID] => 21539896 [userName] => Dana [castleCount] => 9 [NotUpd] => 0 ) )
第二个解码字符串:
Array ( [0] => Array ( [PlayerID] => 17794204 [Trouble] => 2 ) [1] => Array ( [PlayerID] => 21532584 [Trouble] => 0 ) [2] => Array ( [PlayerID] => 21539896 [Trouble] => 0 ) )
之后我们必须merge the two arrays
我们根据key
那是unique
for us.
因此,代码的功能如下。
我已经考虑过PlayerID as the UNIQUE参数和组合了数组。
function merge_json_decoded_arrays($decode_one,$decode_two) {
$data = array();
$arrayAB = array_merge($decode_one,$decode_two);
foreach ($arrayAB as $value) {
$id = $value['PlayerID'];
if (!isset($data[$id])) {
$data[$id] = array();
}
$data[$id] = array_merge($data[$id],$value);
}
return $data;
}
您需要从需要执行以下操作的代码中调用这样的函数array_merge()
运营。
$merged_array = merge_json_decoded_arrays($decode_one,$decode_two);
最后,完整的代码与设置如下所示。
完整代码:
<?php
$array1 = '[
{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000"},
{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0"},
{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0"}
]';
$array2 = '[
{"PlayerID":"17794204","Trouble":"2"},
{"PlayerID":"21532584","Trouble":"0"},
{"PlayerID":"21539896","Trouble":"0"}
]';
$decode_one = json_decode($array1,TRUE);
$decode_two = json_decode($array2,TRUE);
function merge_json_decoded_arrays($decode_one,$decode_two) {
$data = array();
$arrayAB = array_merge($decode_one,$decode_two);
foreach ($arrayAB as $value) {
$id = $value['PlayerID'];
if (!isset($data[$id])) {
$data[$id] = array();
}
$data[$id] = array_merge($data[$id],$value);
}
return $data;
}
$merged_array = merge_json_decoded_arrays($decode_one,$decode_two);
?>
为了查看合并的数组,您需要print_r()
数组并查看它。
数组输出代码:
print_r($merged_array);
Output:
Array ( [17794204] => Array ( [PlayerID] => 17794204 [userName] => Vandiel [castleCount] => 9 [NotUpd] => 1476253231000 [Trouble] => 2 ) [21532584] => Array ( [PlayerID] => 21532584 [userName] => Mayland [castleCount] => 1 [NotUpd] => 0 [Trouble] => 0 ) [21539896] => Array ( [PlayerID] => 21539896 [userName] => Dana [castleCount] => 9 [NotUpd] => 0 [Trouble] => 0 ) )
如果你需要它作为 JSON 输出,你必须json_encode()
得到的array()
并执行操作。
Note:它需要unique ID
as the array key
对于生成的每一行。
JSON 输出代码:
print_r(json_ecode($merged_array));
Output:
{"17794204":{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000","Trouble":"2"},"21532584":{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0","Trouble":"0"},"21539896":{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0","Trouble":"0"}}
最快的执行方法将采取这种方式
您需要解码 json 字符串,然后必须将它们都循环通过foreach()
然后与array()
你需要加入它。
$decode_one = json_decode($array1,TRUE);
$decode_two = json_decode($array2,TRUE);
foreach ($decode_one as $key => $first_value) {
foreach ($decode_two as $key_two => $second_value) {
if($first_value['PlayerID']==$second_value['PlayerID'])
{ $decode_one[$key]['Trouble'] = $second_value['Trouble'];//Here if the key exists it will join the Trouble with the First decoded array }
else {}
}
}
$combined_output = json_encode($decode_one); //This will return the output in json format.
Output:
[{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000","Trouble":"2"},{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0","Trouble":"0"},{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0","Trouble":"0"}]