Quoting from the PHP Manual on Language Operators
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
So if you do
$array1 = ['one', 'two', 'foo' => 'bar'];
$array2 = ['three', 'four', 'five', 'foo' => 'baz'];
print_r($array1 + $array2);
You will get
Array
(
[0] => one // preserved from $array1 (left-hand array)
[1] => two // preserved from $array1 (left-hand array)
[foo] => bar // preserved from $array1 (left-hand array)
[2] => five // added from $array2 (right-hand array)
)
So the logic of + is equivalent to the following snippet:
$union = $array1;
foreach ($array2 as
value) {
if (false === array_key_exists(
union)) {
$union[
value;
}
}
If you are interested in the details of the C-level implementation head to
- php-src/Zend/zend_operators.c
Note, that + is different from how array_merge() would combine the arrays:
print_r(array_merge($array1, $array2));
would give you
Array
(
[0] => one // preserved from $array1
[1] => two // preserved from $array1
[foo] => baz // overwritten from $array2
[2] => three // appended from $array2
[3] => four // appended from $array2
[4] => five // appended from $array2
)
See linked pages for more examples.
Answer from Gordon on Stack OverflowVideos
Quoting from the PHP Manual on Language Operators
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
So if you do
$array1 = ['one', 'two', 'foo' => 'bar'];
$array2 = ['three', 'four', 'five', 'foo' => 'baz'];
print_r($array1 + $array2);
You will get
Array
(
[0] => one // preserved from $array1 (left-hand array)
[1] => two // preserved from $array1 (left-hand array)
[foo] => bar // preserved from $array1 (left-hand array)
[2] => five // added from $array2 (right-hand array)
)
So the logic of + is equivalent to the following snippet:
$union = $array1;
foreach ($array2 as
value) {
if (false === array_key_exists(
union)) {
$union[
value;
}
}
If you are interested in the details of the C-level implementation head to
- php-src/Zend/zend_operators.c
Note, that + is different from how array_merge() would combine the arrays:
print_r(array_merge($array1, $array2));
would give you
Array
(
[0] => one // preserved from $array1
[1] => two // preserved from $array1
[foo] => baz // overwritten from $array2
[2] => three // appended from $array2
[3] => four // appended from $array2
[4] => five // appended from $array2
)
See linked pages for more examples.
The best example I found for using this is in a config array.
$user_vars = array("username"=>"John Doe");
$default_vars = array("username"=>"Unknown", "email"=>"[email protected]");
$config = $user_vars + $default_vars;
The $default_vars, as it suggests, is the array for default values.
The $user_vars array will overwrite the values defined in $default_vars.
Any missing values in $user_vars are now the defaults vars from $default_vars.
This would print_r as:
Array(2){
"username" => "John Doe",
"email" => "[email protected]"
}
I hope this helps!
$arr1 += $arr2 is short for $arr1 = $arr1 + $arr2.
The + array operator does the following:
- Create a new array that contains all the elements of
$arr1and$arr2, except for the next condition. - If both operands have elements with the same key, only the element of
$arr1will be present. - The elements of
$arr2will be after those of$arr1.
This is different from array_merge, which:
- Creates a new array that contains all the elements of
$arr1and$arr2, except for the next condition. - If both operands have elements with the same string key, only the element of
$arr2will 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
$arr2will 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"
}
The + operator in PHP when applied to arrays does the job of array UNION.
$arr += array $arr1;
effectively finds the union of $arr and $arr1 and assigns the result to $arr.