$arr1 += $arr2
is short for $arr1 = $arr1 + $arr2
.
The +
array operator does the following:
- Create a new array that contains all the elements of
$arr1
and $arr2
, except for the next condition.
- If both operands have elements with the same key, only the element of
$arr1
will be present.
- The elements of
$arr2
will be after those of $arr1
.
This is different from array_merge
, which:
- Creates a new array that contains all the elements of
$arr1
and $arr2
, except for the next condition.
- If both operands have elements with the same string key, only the element of
$arr2
will be present.
- Elements with numeric keys will be renumbered from 0, starting with the elements of
$arr1
, and then moving to the elements of $arr2
.
- The elements of
$arr2
will be after those of $arr1
, except the string elements, which will be in the position of the first array in which they appear.
Example:
<?php
$arr1 = array(1 => 'value1.1', 10 => 'value1.2', 's' => 'value1.s');
$arr2 = array(1 => 'value2', 2=> 'value2.2', 's' => 'value2.s');
var_dump(array_merge($arr1,$arr2));
$arr1 += $arr2;
var_dump($arr1);
Result (edited for clarity):
array(5) {
[0] => string(8) "value1.1"
[1] => string(8) "value1.2"
["s"] => string(8) "value2.s"
[2] => string(6) "value2"
[3] => string(8) "value2.2"
}
array(4) {
[1] => string(8) "value1.1"
[10] => string(8) "value1.2"
["s"] => string(8) "value1.s"
[2] => string(8) "value2.2"
}