I have following two arrays
JavaScript
x
$array1 = array (
'MB04' =>
array (
78 =>
array (
2 => '30',
1 => '30',
),
),
);
$array2 = array (
'MB04' =>
array (
78 =>
array (
3 => '25',
),
),
);
I want to merge two arrays so that the final array looks like
JavaScript
$finalArray = array (
'MB04' =>
array (
78 =>
array (
2 => '30',
1 => '30',
3 => '25'
),
),
);
I have used the following approaches but it’s not working:
JavaScript
# Approach 1
var_dump(array_merge($array1, $array2));
# Approach 2
var_dump($array1 + $array2);
# Approach 3
var_dump(array_merge_recursive($array1, $array2));
I want to achieve the final array with the native function (minimal code) if possible.
Advertisement
Answer
Finally found the solution using array_replace_recursive
JavaScript
# Approach 4
var_dump(array_replace_recursive($array1, $array2));