sgkdnay sgkdnay - 1 month ago 23
JSON Question

php merge json arrays

array 1:

[
{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000"},
{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0"},
{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0"}
]


array 2:

[
{"PlayerID":"17794204","Trouble":"2"},
{"PlayerID":"21532584","Trouble":"0"},
{"PlayerID":"21539896","Trouble":"0"}
]


Those both were pulled from mySQL database using fetch_assoc_all()

I tried merge_array, merge_array_recursive, json_decode(xx,true) and all kinds of things I could think on top of my head and elsewhere via google. I'm looking for a way to merge both array1, array2 into something like:

[
{"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"}
]


PlayerID are always unique. Hope to hear what insight I could do to merge those 2 arrays (array1,array2)

(Additonal/Edit)
For those wondering what's mySQL looks like (I couldn't wrap my head around on JOIN statement):

$mSQL = 'SELECT nPlayer.PlayerID,userName,castleCount,IF(LastUpdate < (UNIX_TIMESTAMP() - 3720),LastUpdate*1000,0) NotUpd';
$mSQL .= ' FROM nPlayer';
$mSQL .= ' LEFT JOIN nMains ON nMains.mID = nPlayer.mID';
$mSQL .= ' WHERE nMains.Main = "'.$M.'" AND nMains.Pass = "'.md5($P).'" AND nMains.Server = "'.$S.'"';
$mSQL .= ' ORDER BY nPlayer.PlayerID';

$mSQL = 'SELECT nCity.PlayerID,SUM(IF(Wartown > 0,1,0))+SUM(IF(support < 100,1,0))) Trouble';
$mSQL .= ' FROM nCity';
$mSQL .= ' INNER JOIN nPlayer ON nPlayer.PlayerID = nCity.PlayerID AND nPlayer.mID = nCity.mID';
$mSQL .= ' INNER JOIN nMains ON nMains.mID = nPlayer.mID';
$mSQL .= ' WHERE nMains.Main = "'.$M.'" AND nMains.Pass = "'.md5($P).'" AND nMains.Server = "'.$S.'"';
$mSQL .= ' GROUP BY nCity.PlayerID';

Answer

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"}]
Comments