Similarities and differences between the two ways of merging two arrays

Source: Internet
Author: User

This article discusses two ways to combine arrays: $a + $b, array_merge ($a, $b)

operator +, insert the last array on the basis of the previous number, if there is the same discard, and Array_merge is the first argument based on the insertion of the latter parameter, if there is the same key used after an overlay. Examples are as follows:

php > $a = array (' A ' = ' 1 ', ' b ' = ' 2 ', ' c ' = ' 3 ');p hp > $b = Array (' C ' =>3, ' d ' = ' 4 ', ' e ' = ' 5 ');p hp > $c = $a + $b;p hp > $d = Array_merge ($a, $b);p hp > Var_dump ($c, $d), Array (5) {  ' a ' + =  string (1) "1"  B ' + =  string (1) "2"  ' C ' and "= =  string (1)" 3 "  ' d ' = = =  String (1)" 4 "  ' e ' = =  string (1 ) "5"}array (5) {  ' a ' + =  string (1) "1"  ' b ' = +  string (1) "2"  ' C ' + =  int (3)  ' d ' +  string (1) "4"  ' e ' + =  string (1) "5"}php > $e = Array_merge ($b, $a);p hp > Var_dump ($e); Array (5) {  ' c ' + =  string (1) "3"  ' d ' + =  string (1) "4"  ' e ' = +  string (1) "5"  ' a ' and ' =  string (1) ' 1 ' ' b ' and ' = '  string (1) ' 2 '}php > $f = $b + $a;p hp > Var_dump ($f), Array (5) {  ' c ' + =  int (3)  ' d ' and ' +  string (1) "4"  ' e ' = =  string (1) "5"  ' a ' + =  String (1) "1"  ' b ' = =  string (1) "2"}

When both sides are indexed arrays, using array_merge may cause an index reset, as in the following scenario:

php > $a = array ();p hp > $b = Array (1=> ' data ');p hp > $c = $a + $b;p hp > $d = Array_merge ($a, $b);p hp > Var_ Dump ($c, $d); Array (1) {  [1] = =  string (4) "Data"}array (1) {  [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:

php > $a = array (1=> ' one ',2=> ', ',3=> ' three ');p hp > $b = Array (3=> ' three ',4=> ' four ',5=> ' five ' );p hp > $c = $a + $b;p hp > $d = Array_merge ($a, $b);p hp > Var_dump ($c, $d); Array (5) {  [1] =  string (3) "on E "  [2] = =  string (3)" ""  [3] = =  string (5) "Three"  [4] = =  string (4) "Four"  [5] = >  String (4) "Five"}array (6) {  [0] = =  string (3) "One"  [1] and  string (3) "One"  [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.