In php, it seems that both methods can merge arrays, and I found that array_merge is followed by the front, while + is followed by the front, for example, the following code {code ...} output {code ...} has anyone concluded that there is any difference between the two? In php, it seems that both methods can merge arrays, and I found that
array_mergeIs to overwrite the front, and
+Is to overwrite the following code.
$a = array('a' => 1, 'b' => 2, 'c' => 3);$b = array('b' => 5, 'c' => 6, 'd' => 7);print_r(array_merge($a, $b));print_r($a + $b);
Output
Array( [a] => 1 [b] => 5 [c] => 6 [d] => 7)Array( [a] => 1 [b] => 2 [c] => 3 [d] => 7)
Has anyone concluded that there is any difference between the two?
Reply content:
In php, it seems that both methods can merge arrays, and I found thatarray_mergeIs to overwrite the front, and+Is to overwrite the following code.
$a = array('a' => 1, 'b' => 2, 'c' => 3);$b = array('b' => 5, 'c' => 6, 'd' => 7);print_r(array_merge($a, $b));print_r($a + $b);
Output
Array( [a] => 1 [b] => 5 [c] => 6 [d] => 7)Array( [a] => 1 [b] => 2 [c] => 3 [d] => 7)
Has anyone concluded that there is any difference between the two?
Array_merge ():
If the input array contains the same string key name, the value after the key name overwrites the previous value. (Your 1567) if the array contains a number key name (you can try it before, but it's not me), the subsequent values will not overwrite the original values, but be appended to the back. (The number key name will be reassigned and will always start from scratch ..)
Merge arrays with "+:
If the array contains the same string key name (no matter whether it is a number or not), the first value that appears is returned as the final result, the values of the following arrays with the same key names are discarded. (Your 1237)
Many people have concluded that Google was the only solution to this problem ..
In fact, the manual is clearly written:
Array array_merge (array $ array1 [, array $ array2 [, array $...])
Array_merge () combines the units of one or more arrays. The values in an array are appended to the values of the previous array. Returns an array of results.
If the input array contains the same string key name, the value after the key name overwrites the previous value. However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.
If only one array is assigned and the array is indexed by number, the key name is re-indexed continuously.
First, my PHP version is 5.3.3.
var_dump(array_merge(array('key'=>'value'),array(-1=>'-1',0=>'0')));
Get:
array(3) { ["key"]=> string(5) "value" [0]=> string(2) "-1" [1]=> string(2) "0" }
That is to say, although two arrays are given, one of them is not an index array, array_emrge will re-index the array. However, "+" won't.