Union two arrays in PHP with the same structure
Solution 1:
Use array_merge
. By the way, indexes 0
and 1
are redundant at $a
and $b
in this example.
<?php
$a = [
0 => [
'type' => "t1",
'name' => "John",
],
1 => [
'type' => "t1",
'name' => "Jane",
]
];
$b = [
0 => [
'surname' => "Doe",
],
1 => [
'surname' => "Black",
]
];
$c = [];
// Assuming that count of $a and $b are equal.
for ($i = 0; $i < count($a); ++$i) {
$c[$i] = array_merge($a[$i], $b[$i]);
}
print_r($c);