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