Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

php menggabungkan array json

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 dengan json_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"}]


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana cara mengaktifkan indeks besar di MariaDB 10?

  2. Masalah menampilkan hasil kueri

  3. Pembaruan/edit Laravel menggunakan foreach loop

  4. Hapus Doctrine QueryBuilder dengan bergabung

  5. Menambahkan baris ke tabel dengan kolom unik:Dapatkan nilai ID yang ada plus yang baru dibuat