Created
June 13, 2022 05:18
-
-
Save ammarfaizi2/8691ce0c4d6998262272be353c9d9604 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Link: https://fb.com/10159955933216101 | |
*/ | |
$arr1 = array( | |
array( | |
'id'=> 1, | |
'nama' => 'anto', | |
'alamat' => 'jln 101' | |
), | |
array( | |
'id'=> 2, | |
'nama' => 'joko', | |
'alamat' => 'jln 102' | |
), | |
array( | |
'id'=> 3, | |
'nama' => 'fra', | |
'alamat' => 'jln 103' | |
), | |
); | |
$arr2 = array( | |
array( | |
'id' => 1, | |
'ttl' => '2022-01-01' | |
), | |
array( | |
'id' => 2, | |
'ttl' => '2012-01-03' | |
) | |
); | |
$arr3 = array( | |
array( | |
'id' => 1, | |
'agama' => 'islam' | |
), | |
array( | |
'id' => 3, | |
'agama' => 'budha' | |
) | |
); | |
/* | |
* Buat hash table dengan isi key semua "id" di $arr1, map ke indexnya. | |
*/ | |
$map = []; | |
foreach ($arr1 as $k => $v) { | |
$map[$v["id"]] = $k; | |
} | |
/* | |
* Iterate $arr2 dan $arr3, merge ke $arr1 berdasarkan "id". | |
*/ | |
$p = NULL; | |
foreach (array($arr2, $arr3) as $vv) { | |
foreach ($vv as $v) { | |
if (!isset($map[$v["id"]])) { | |
continue; | |
} | |
$id = $v["id"]; | |
unset($v["id"]); | |
$p = &$arr1[$map[$id]]; | |
$p = array_merge($p, $v); | |
} | |
} | |
unset($p, $map); | |
print_r($arr1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment