Difference between the plus signs of the array_merge function operator in PHP array merging

Source: Internet
Author: User
PHP merge array array_merge function operator plus sign and difference, read PHP merge array array_merge function operator plus sign and difference, "+" operator and array_merge (): arrayarray_merge (arrayarray1, arrayarray2 [, array...]) you can merge multiple arrays, but you have used

The "+" operator and array_merge (): array array_merge (array array1, array array2 [, array...]) can combine multiple arrays, but there is a little difference in use.

Array_merge is described in the reference manual as follows:
Array_merge () combines two 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.
The two differences are:
1. when the array KEY name is a numeric KEY, when the two arrays to be merged have a numeric KEY with the same name, using array_merge () will not overwrite the original value, when "+" is used to merge arrays, the first value will be returned as the final result, and the values of the following arrays with the same key name will be discarded (note: instead of overwriting, the value that appears first is retained ). Example:

$ Array1 = array (1 => '0 ');
$ Array2 = array (1 => "data ");
$ Result1 = $ array2 + $ array1;/* The result is a value of $ array2 */
Print_r ($ result );
$ Result = $ array1 + $ array2;/* The result is a value of $ array1 */
Print_r ($ result );
$ Result3 = array_merge ($ array2, $ array1);/* The result is the value of $ array2 and $ array1, and the key name is reassigned */
Print_r ($ result3 );
$ Result4 = array_merge ($ array1, $ array2);/* The result is $ array1 and $ array2, and the key name is reassigned */
Print_r ($ result4 );

Output result:
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 is a number, but array_merge () will overwrite the value of the previous same key name.
Example:
$ Array1 = array ('asd '=> '0 ');
$ Array2 = array ('asd '=> "data ");
$ Result1 = $ array2 + $ array1;/* The result is a value of $ array2 */
Print_r ($ result );
$ Result = $ array1 + $ array2;/* The result is a value of $ array1 */
Print_r ($ result );
$ Result3 = array_merge ($ array2, $ array1);/* The result is $ array1 */
Print_r ($ result3 );
$ Result4 = array_merge ($ array1, $ array2);/* The result is $ array2 */
Print_r ($ result4 );


Output result:

Array ([asd] => data)
Array ([asd] => 0)
Array ([asd] => 0)
Array ([asd] => data)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.