First reindex $array2
using id
. array_column
can make this easy:
$array2 = array_column($array2, null, 'id');
Then overwrite the keys in $array2
for each value in $array1
that has a matching 'id'
key.
foreach ($array1 as $key => $value) {
$array2[$value['id']] = $value;
}