PHP merge array + and array_merge difference analysis. 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, note 1) when the key name is a number, array_merge () do not overwrite the original value. 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. Note that
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.
The 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
The 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)
When the key name is a number, array_merge () will not overwrite the original value...