Penjelasan Detail
Anda dapat bergabung dengan array JSON berdasarkan nilai kunci yang Anda peroleh asalkan Anda harus memberikan di bawah kunci mana Anda harus bergabung denganjson_array()
.
Saya akan mempertimbangkan json_objects
sebagai berikut berdasarkan kode 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"}
]';
?>
Oleh karena itu untuk menggabungkan json_objects kita harus terlebih dahulu menggunakan json_decode()
untuk kedua array yang telah kita peroleh.
$decode_one = json_decode($array1,TRUE);
$decode_two = json_decode($array2,TRUE);
Karenanya output untuk json_decoded()
string akan menjadi sebagai berikut.
String Dekode Pertama:
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 ) )
String Dekode Kedua:
Array ( [0] => Array ( [PlayerID] => 17794204 [Trouble] => 2 ) [1] => Array ( [PlayerID] => 21532584 [Trouble] => 0 ) [2] => Array ( [PlayerID] => 21539896 [Trouble] => 0 ) )
Maka fungsi dari kode tersebut adalah sebagai berikut.
Saya telah mempertimbangkan PlayerID sebagai UNIK Parameter dan telah menggabungkan larik.
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;
}
Anda perlu memanggil fungsi seperti ini dari kode di mana Anda perlu melakukan array_merge()
operasi.
$merged_array = merge_json_decoded_arrays($decode_one,$decode_two);
Akhirnya kode lengkapnya muncul seperti ini dengan setup.
Kode Lengkap:
<?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);
?>
Untuk melihat larik gabungan, Anda perlu print_r()
array dan melihatnya.
Kode Keluaran Array:
print_r($merged_array);
Keluaran:
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 ) )
Jika Anda membutuhkannya sebagai output JSON, Anda harus json_encode()
array()
yang diperoleh dan melakukan operasi.
Kode Keluaran JSON:
print_r(json_ecode($merged_array));
Keluaran:
{"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"}}
Metode Eksekusi Tercepat akan menggunakan cara ini
Anda perlu mendekode json_strings dan kemudian Anda harus mengulang keduanya melalui foreach()
lalu gabungkan dengan array()
yang Anda butuhkan untuk bergabung dengannya.
$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.
Keluaran:
[{"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"}]