Similarities and differences between the two ways of merging two arrays

Source: Internet
Author: User

Two ways to merge arrays: $a + $b, array_merge ($a, $b)

Merging indexed arrays using the operator +, duplicate indexes are discarded, and duplicate indexes with Array_merge () are reset, as in the following scenario:

1php > $a =Array (); 2 3php > $b = Array (1='Data'); 4 5php > $c = $a +$b; 6 7php > $d =array_merge ($a, $b); 8 9php >var_dump ($c, $d); Ten  One //Output Results A  -Array1) {  [1] =string(4)"Data" }   -  theArray1) {  [0] =string(4)"Data"}

Therefore, when using + to merge two indexed arrays, the values may be discarded, and array_merge will not, resetting the duplicate index:

1php > $a = Array (1=' One',2=' Both',3='three'); 2php > $b = Array (3='three',4=' Four',5='Five'); 3php > $c = $a +$b; 4php > $d =array_merge ($a, $b); 5php >var_dump ($c, $d); 6Array5) {  7[1] =string(3)" One"  8[2] =string(3)" Both"  9[3] =string(5)"three"  Ten[4] =string(4)" Four"   One[5] =string(4)"Five"   A }   -  -Array6) {   the[0] =string(3)" One"   -[1] =string(3)" Both"   -[2] =string(5)"three"   -[3] =string(5)"three"   +[4] =string(4)" Four"   -[5] =string(4)"Five"   +}

Similarities and differences between the two ways of merging two arrays

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.