$test = array('hi');
$test += array('test','oh');
var_dump($test);
What does +
mean for array in PHP?
It basically means
outputs:
See linked page for more examples. |
|||||||||||||
|
Carefull with numeric keys, if they should be preserved or if you don't want to loose anything
union
merge
|
|||
|
Array ( [0] => example [1] => test ) |
|||
|
The best example I found for using this is in a config array.
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:
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. |
|||||
|
The + operator appends elements of remaining keys from the right handed array to the left handed, whereas duplicated keys are NOT overwritten.
When executed, this script will print the following: Union of $a and $b: array(3) { ["a"]=> string(5) "apple" ["b"]=> string(6) "banana" ["c"]=> string(6) "cherry" } Union of $b and $a: array(3) { ["a"]=> string(4) "pear" ["b"]=> string(10) "strawberry" ["c"]=> string(6) "cherry" } not exactly like Note that the plus operator for arrays ‘+’ is only one-dimensional, and is only suitable for simple arrays. read more : http://www.vancelucas.com/blog/php-array_merge-preserving-numeric-keys/ |
|||||||||||
|
+=
and the accepted answer had+
. From my testing they seem to behave the same. – user151841 Aug 24 '12 at 17:24