Detailed Explanation
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().
I am going to consider the json_objects as follows based on the 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"}
]';
?>
Hence inorder to merget the json_objects we have to first use json_decode() for the both the arrays that we have obtained.
$decode_one = json_decode($array1,TRUE);
$decode_two = json_decode($array2,TRUE);
Hence the output for the json_decoded() string will be as follows.
First Decoded 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 ) )
Second Decoded String:
Array ( [0] => Array ( [PlayerID] => 17794204 [Trouble] => 2 ) [1] => Array ( [PlayerID] => 21532584 [Trouble] => 0 ) [2] => Array ( [PlayerID] => 21539896 [Trouble] => 0 ) )
After that we have to merge the two arrays that we have obtained based on the key that is unique for us.
Hence the function for the code is as follows.
I have considered the PlayerID as the UNIQUE Parameter and has combined the array.
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;
}
You need to call the function like this from the code where you need to perform the array_merge() operations.
$merged_array = merge_json_decoded_arrays($decode_one,$decode_two);
Finally the full code appears like this with the setup.
Full 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);
?>
In order to view the merged array you need to print_r() the array and view it.
Array Output Code:
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 ) )
If you need it as the JSON output you have to json_encode() the obtained array() and perform the operations.
Note: It takes the unique ID as the array key for each row that is been generated.
JSON Output Code:
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"}}
Fastest Execution Method will take up this way
You need to decode the json_strings and then you have to llop both of them through the foreach() and then combine with the array() that you need to join with it.
$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"}]