PHP looks like both of these methods can be combined array, I found a point, is
array_merge
Is behind the cover of the front, and
+
Is the previous overlay, such as the following code
$a = Array (' a ' = = 1, ' b ' = = 2, ' c ' = = 3); $b = Array (' b ' = = 5, ' C ' + 6, ' d ' + 7);p Rint_r (Array_merge ($a , $b));p Rint_r ($a + $b);
On the output
Array ( [A] = 1 [b] = 5 [c] = 6 [d] + 7) Array ( [a] + = 1 [b] = 2 [c] => ; 3 [d] = 7)
Has anyone summed up the difference between the two?
Reply content:
PHP looks like both of these methods can be combined array, I found a point, is the array_merge
back cover the front, but +
the front cover behind, such as the following code
$a = Array (' a ' = = 1, ' b ' = = 2, ' c ' = = 3); $b = Array (' b ' = = 5, ' C ' + 6, ' d ' + 7);p Rint_r (Array_merge ($a , $b));p Rint_r ($a + $b);
On the output
Array ( [A] = 1 [b] = 5 [c] = 6 [d] + 7) Array ( [a] + = 1 [b] = 2 [c] => ; 3 [d] = 7)
Has anyone summed up the difference between the two?
Array_merge ():
If the input array has the same string key name, the value following the key name overrides the previous value. (your 1567) if the array contains a numeric key name (you can try it and toss me before), the value behind will not overwrite the original value, but append it to the back. (The numeric key name is reassigned and will always become zero-based.) )
For merging arrays with "+":
If the array has the same string key name (whether it is not a number), the first occurrence is returned as the final result, and the values of the following array with the same key name are "discarded". (Your 1237)
A lot of people have summed up, when this problem is Google God rescued.
In fact, the handbook is very clear:
Array Array_merge (array $array 1 [, Array $array 2 [, array $ ...])
Array_merge () Merges the cells of one or more arrays, and the values in an array are appended to the previous array. Returns an array as the result.
If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, subsequent values will not overwrite the original values, but are appended to the back.
If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential manner.
First of all, my PHP version is 5.3.3
var_dump(array_merge(array('key'=>'value'),array(-1=>'-1',0=>'0')));
Get:
That is, although two arrays are given, one of which is not an indexed array, Array_emrge will still index the index array again. However, "+" will not.