I have some code that appears to merge the data from two arrays using +=, but it doesn’t include all of the elements in the element. How does it work?
Example:
$test = array('hi');
$test += array('test', 'oh');
var_dump($test);
Output:
array(2) {
[0]=>
string(2) "hi"
[1]=>
string(2) "oh"
}
What does + mean when used on arrays in PHP?
Quoting from the PHP Manual on Language Operators
So if you do
You will get
So the logic of
+is equivalent to the following snippet:If you are interested in the details of the C-level implementation head to
Note, that
+is different from howarray_merge()would combine the arrays:would give you
See linked pages for more examples.