What we are introducing to you today isThe Array_merge is described in the reference manual as follows:
The PHP function Array_merge () merges two or more arrays of cells, 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.
The difference between the PHP function Array_merge () and the plus operator two is:
1. When the array key name is a numeric key name, the two arrays to be merged have the same name as the number key, using Array_merge () will not overwrite the original value, while using "+" to merge the array will return the first occurrence of the value as the final result, and the following array with the same key name of those values "discard" (note: Instead of overwriting, it retains the first value that appears). Example:
- $ Array array1 = Array (1=>' 0 ');
- $ Array array2 = Array (1=> "Data");
- $ RESULT1 = $array 2 + $array 1;/* result is the value of $array2 * /
- Print_r ($result);
- $ result = $array 1 + $array 2;/* The result is a value of $array1 * /
- Print_r ($result);
- $ RESULT3 = Array_merge ($array 2, $array 1);/* The result is a value of $array2 and $array1, and the key name is reassigned */
- Print_r ($result 3);
- $ RESULT4 = Array_merge ($array 1, $array 2);/* The result is a value of $array1 and $array2, and the key name is reassigned */
- Print_r ($result 4);
The output is:
- Array ([1] => data)
- Array ([1] => 0)
- Array (
- [0] => data
- [1] => 0
- )
- Array
- (
- [0] => 0
- [1] => data
- )
2. When the same array key name is a character, the "+" operator is the same as the key name, but the PHP function Array_merge () overrides the previous value of the same key name.
Example:
- $ Array array1 = Array (' ASD ' =>' 0 ');
- $ Array array2 = Array (' ASD ' => "Data");
- $ RESULT1 = $array 2 + $array 1;/* result is the value of $array2 * /
- Print_r ($result);
- $ result = $array 1 + $array 2;/* The result is a value of $array1 * /
- Print_r ($result);
- $ RESULT3 = Array_merge ($array 2, $array 1);/* results are $array1*/
- Print_r ($result 3);
- $ RESULT4 = Array_merge ($array 1, $array 2);/* results are $array2*/
- Print_r ($result 4);
The output is:
- Array ([ASD] => data)
- Array ([ASD] => 0)
- Array ([ASD] => 0)
- Array ([ASD] => data)
These are the differences between the PHP function Array_merge () and the plus operator in actual use.
http://www.bkjia.com/PHPjc/446400.html www.bkjia.com true http://www.bkjia.com/PHPjc/446400.html techarticle What we are introducing to you today is array_merge in the reference manual as follows: the PHP function Array_merge () merges two or more arrays of cells, and the values in an array are appended to ...