$test = array('hi');
$test += array('test','oh');
var_dump($test);
What does +
mean for array in PHP?
In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.
=== is strict equality, not an identity operator. When objects are ran through a strict equality operator, they are checked to see if they point to the same reference. As I explained earlier, each time you use new Array or [] that a brand new object will be created, each being a new and different reference.
Union of arrays The Union operator appends the right-hand array appended to left-hand array. ; If a key exists 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.
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 .
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 $key => $value) {
if (false === array_key_exists($key, $union)) {
$union[$key] = $value;
}
}
If you are interested in the details of the C-level implementation head to
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!
This operator takes the union of two arrays (same as array_merge, except that with array_merge duplicate keys are overwritten).
The documentation for array operators is found here.
Carefull with numeric keys, if they should be preserved or if you don't want to loose anything
$a = array(2 => "a2", 4 => "a4", 5 => "a5");
$b = array(1 => "b1", 3 => "b3", 4 => "b4");
union
print_r($a+$b);
Array
(
[2] => a2
[4] => a4
[5] => a5
[1] => b1
[3] => b3
)
merge
print_r(array_merge($a, $b));
Array
(
[0] => a2
[1] => a4
[2] => a5
[3] => b1
[4] => b3
[5] => b4
)
The +
operator produces the same results as array_replace(). However since the operator arguments are reversed, the ordering of the resulting array may also be different.
Expanding on another example from this page:
$array1 = array('one', 'two', 'foo' => 'bar');
$array2 = array('three', 'four', 'five', 'foo' => 'baz');
print_r($array1 + $array2);
print_r(array_replace($array2, $array1)); //note reversed argument order
outputs:
Array
(
[0] => one // preserved from $array1
[1] => two // preserved from $array1
[foo] => bar // preserved from $array1
[2] => five // added from $array2
)
Array
(
[0] => one // preserved from $array1
[1] => two // preserved from $array1
[2] => five // added from $array2
[foo] => bar // preserved from $array1
)
- Array plus operation treats all array as assoc array.
- When key conflict during plus, left(previous) value will be kept
I post the code below to make things clear.
$a + $b = array_plus($a, $b)
function array_plus($a, $b){
$results = array();
foreach($a as $k=>$v) if(!isset($results[$k]))$results[$k] = $v;
foreach($b as $k=>$v) if(!isset($results[$k]))$results[$k] = $v;
return $results;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With