Analysis on the differences between PHP combined array + and array_merge

Source: Internet
Author: User

The main difference is that if the same key name appears in two or more arrays, the key name can be divided into strings or numbers.

1) when the key name is a number, array_merge () will not overwrite the original value, but + merges the array and returns the first value as the final result, the values of the following arrays with the same key names are discarded (not overwritten)

2) When the key name is a character, + still returns the first value as the final result, and discards the values with the same key name in the subsequent array, but array_merge () at this time, the values with the same key names will be overwritten.

Note that the array key form 'number' is equivalent to a number.
Copy codeThe Code is as follows:
$ A = array ('A', 'B ');
$ B = array ('C', 'D ');
$ C = $ a + $ B;
Var_dump ($ );
Var_dump (array_merge ($ a, $ B ));

$ A = array (0 => 'A', 1 => 'B ');
$ B = array (0 => 'C', 1 => 'B ');
$ C = $ a + $ B;
Var_dump ($ c );
Var_dump (array_merge ($ a, $ B ));

$ A = array ('A', 'B ');
$ B = array ('0' => 'C', 1 => 'B ');
$ C = $ a + $ B;
Var_dump ($ c );
Var_dump (array_merge ($ a, $ B ));

$ A = array (0 => 'A', 1 => 'B ');
$ B = array ('0' => 'C', '1' => 'B ');
$ C = $ a + $ B;
Var_dump ($ c );
Var_dump (array_merge ($ a, $ B ));

Result
Copy codeThe Code is as follows:
Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
2 => string 'C' (length = 1)
3 => string 'D' (length = 1)

Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)

Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
2 => string 'C' (length = 1)
3 => string 'B' (length = 1)

Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)

Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
2 => string 'C' (length = 1)
3 => string 'B' (length = 1)

Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)

Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
2 => string 'C' (length = 1)
3 => string 'B' (length = 1)

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.