I want to combine two multidimensional arrays in PHP.
print_r($array_a):
Array
(
[0] => Array
(
[0] => A
[1] => 0
[2] => 1047
)
[1] => Array
(
[0] => B
[1] => 0
[2] => 279
)
[2] => Array
(
[0] => C
[1] => 0
[2] => 68
)
[3] => Array
(
[0] => D
[1] => 0
[2] => 4
)
)
print_r($array_b):
Array
(
[0] => Array
(
[0] => A
[1] => 76
[2] => 0
)
[1] => Array
(
[0] => B
[1] => 170
[2] => 0
)
[2] => Array
(
[0] => C
[1] => 15
[2] => 0
)
[3] => Array
(
[0] => D
[1] => 210
[2] => 0
)
[4] => Array
(
[0] => E
[1] => 287
[2] => 0
)
)
Then merge it, the result should be like this:
Array
(
[0] => Array
(
[0] => A
[1] => 76
[2] => 1047
)
[1] => Array
(
[0] => B
[1] => 170
[2] => 279
)
[2] => Array
(
[0] => C
[1] => 15
[2] => 68
)
[3] => Array
(
[0] => D
[1] => 210
[2] => 4
)
[4] => Array
(
[0] => E
[1] => 287
[2] => 0
)
)
So the logic key is to merge the two array based on the first value of each array(A, B, C, D). And if there's an array that belong only in one of the array (for example, "E" on the array_b), I just add them. I tried this:
foreach($array_a as $a=>$array_now){
foreach($array_b as $b=>$array_before){
if($array_now[0] == $array_before[0]){
$array_a[$a] = [$array_before[0], $array_before[1], $array_now[2]];
}
}
}
But I can't seem to add the missing array (the 5th array in $array_b which contain 'E').