Is there any difference
Is there any difference between array_merge and array + array. Both return the same thing.
$merge = array_merge($arrayCar, $arrayBike);
$merged = $arrayCar+ $arrayBike;
- 4 years ago
The difference is:
+ operator takes the union of the two arrays, whereas the
array_merge function takes the union BUT the duplicate keys are overwritten).
here's a simple illustrative test:
$ar1 = [ 0 => '1-0', 'a' => '1-a', 'b' => '1-b' ]; $ar2 = [ 0 => '2-0', 1 => '2-1', 'b' => '2-b', 'c' => '2-c' ]; print_r($ar1+$ar2); print_r(array_merge($ar1,$ar2));
with the result:
Array (  => 1-0 [a] => 1-a [b] => 1-b  => 2-1 [c] => 2-c ) Array (  => 1-0 [a] => 1-a [b] => 2-b  => 2-0  => 2-1 [c] => 2-c )