I used ideas of Dave and mArm.ch and came up with a pretty simple solution.
The goal is to merge associative $array1
and $array2
and overwrite $array1
with $array2
.
The code:
foreach (array_keys($array2) as $key) {
if (isset($array1[$key]) unset($array1[$key]);
}
$desired_array = $array1 + $array2;
So basically I delete keys from one array when there's an overlap and then I can simply add them.
Please make sure that both arrays isset()
and is_array()
. Otherwise you get a fatal error.