sql >> Base de Datos >  >> RDS >> Mysql

php fusionar matrices json

Explicación detallada

Puede unirse a la matriz JSON en función del valor de la clave que obtenga, siempre que tenga que indicar bajo qué clave debe unirse a json_array() .

Voy a considerar los json_objects como sigue basado en el código 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"}
]';
?>

Por lo tanto, para fusionar los json_objects, primero debemos usar json_decode() para las dos matrices que hemos obtenido.

$decode_one = json_decode($array1,TRUE);
$decode_two = json_decode($array2,TRUE);

De ahí la salida para json_decoded() cadena será la siguiente.

Primera cadena decodificada:

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 ) ) 

Segunda cadena decodificada:

Array ( [0] => Array ( [PlayerID] => 17794204 [Trouble] => 2 ) [1] => Array ( [PlayerID] => 21532584 [Trouble] => 0 ) [2] => Array ( [PlayerID] => 21539896 [Trouble] => 0 ) )

Por lo tanto, la función del código es la siguiente.

He considerado el PlayerID como el ÚNICO Parámetro y ha combinado la matriz.

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;
  }

Debe llamar a la función como esta desde el código donde necesita realizar el array_merge() operaciones.

$merged_array = merge_json_decoded_arrays($decode_one,$decode_two);

Finalmente, el código completo aparece así con la configuración.

Código completo:

<?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);
?>

Para ver la matriz combinada, debe print_r() la matriz y verla.

Código de salida de matriz:

print_r($merged_array);

Salida:

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 ) )

Si lo necesita como salida JSON, debe json_encode() el array() obtenido y realizar las operaciones.

Código de salida JSON:

print_r(json_ecode($merged_array));

Salida:

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

El método de ejecución más rápido seguirá este camino

Necesitas decodificar las json_strings y luego tienes que pasarlas a través de foreach() y luego combinar con el array() que necesitas unirte a él.

$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.

Salida:

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