The combination of two arrays in PHP can use + or array_merge, but there is still a difference between them, and these differences if you do not know clearly the project will be fatal!
The main difference is that if the same key name appears in two or more arrays, the key name is a string or a number, you need to be aware
1) When the key name is a number , the value after Array_merge () will not overwrite the original value, but append it to the back, but the + merge array will return the first occurrence as the final result, and the values of the following array with the same key name "Discard" (not overwrite)
2) When the key name is a string , Array_merge () overwrites the previous value of the same key name, but + still returns the first occurrence as the final result, and "discards" the values of the following array with the same key name (not overwrite).
It is important to note that the array key form ' number ' is equivalent to the number
$a = Array (' A ', ' B '); $b = Array (' C ', ' d '); $c = $a + $b; var_dump ($a); Var_dump (Array_merge ($a, $b));//output: Array 0 = Stri Ng ' A ' (length=1) 1 = string ' B ' (length=1) array 0 = String ' a ' (length=1) 1 = string ' B ' (length=1) 2 =&G T String ' C ' (length=1) 3 = = String ' d ' (length=1) ++++++++++++++++++++++++++++++++++++++++++ $a = Array (0 = ' a ', 1 = ' B '), $b = Array (0 = ' C ', 1 = ' B '), $c = $a + $b; Var_dump ($c); Var_dump (Array_merge ($a, $b));//output: 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) ++++++++++++++++++++++++++++++++++++++++++ $a = array (' A ', ' B ' ); $b = Array (' 0 ' = ' c ', 1 = ' B '); $c = $a + $b; Var_dump ($c); Var_dump (Array_merge ($a, $b));//output: 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) ++++++++++++++++++++++++++++++++++++++++++ $a = Array (0 = ' a ', 1 = ' B '); $b = Arra Y (' 0 ' = ' C ', ' 1 ' = ' B '); $c = $a + $b; Var_dump ($c); Var_dump (Array_merge ($a, $b)); output: Array 0 = String ' a ' (lengt h=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)
Merging techniques for multiple arrays
$a = Array (' 1001 ', ' 1002 '); $b = Array (' 1002 ', ' 1003 ', ' 1004 '); $c = Array (' 1003 ', ' 1004 ', ' 1005 '); $d = count (Array_flip ($a) + Array_flip ($b) + array_flip ($c));
Extended reading:
PHP Merges values of 2 numeric key arrays
PHP array merging + vs. Array_merge analysis & merging of multiple arrays to remove heavy skills