Gedetailleerde uitleg
U kunt deelnemen aan de JSON-array op basis van de sleutelwaarde die u verkrijgt, op voorwaarde dat u moet opgeven onder welke sleutel u moet deelnemen aan dejson_array()
.
Ik ga de json_objects
. overwegen als volgt op basis van de PHP-code.
<?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"}
]';
?>
Daarom moeten we om de json_objects samen te voegen eerst json_decode()
gebruiken voor beide de arrays die we hebben verkregen.
$decode_one = json_decode($array1,TRUE);
$decode_two = json_decode($array2,TRUE);
Vandaar de uitvoer voor de json_decoded()
string zal als volgt zijn.
Eerste gedecodeerde string:
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 ) )
Tweede gedecodeerde string:
Array ( [0] => Array ( [PlayerID] => 17794204 [Trouble] => 2 ) [1] => Array ( [PlayerID] => 21532584 [Trouble] => 0 ) [2] => Array ( [PlayerID] => 21539896 [Trouble] => 0 ) )
Daarom is de functie voor de code als volgt.
Ik heb de PlayerID . overwogen als de UNIEKE Parameter en heeft de array gecombineerd.
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;
}
Je moet de functie op deze manier aanroepen vanuit de code waar je de array_merge()
moet uitvoeren operaties.
$merged_array = merge_json_decoded_arrays($decode_one,$decode_two);
Eindelijk verschijnt de volledige code zo bij de installatie.
Volledige code:
<?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);
?>
Om de samengevoegde array te bekijken moet je print_r()
de array en bekijk deze.
Array-uitvoercode:
print_r($merged_array);
Uitvoer:
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 ) )
Als je het nodig hebt als de JSON-uitvoer, moet je json_encode()
de verkregen array()
en voer de bewerkingen uit.
JSON-uitvoercode:
print_r(json_ecode($merged_array));
Uitvoer:
{"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"}}
De snelste uitvoeringsmethode wordt op deze manier gevolgd
Je moet de json_strings decoderen en dan moet je ze allebei door de foreach()
halen en vervolgens combineren met de array()
dat je erbij moet zijn.
$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.
Uitvoer:
[{"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"}]