You can use + or array_merge to merge two arrays in PHP, but there are still differences between the two. It is necessary to clearly understand the differences in the two Processing Methods for rapid development of the project.
The main difference is that if the same key name appears in two or more arrays, pay attention to the following two points:
First, you must note that the array key names in php can be divided into strings (associated arrays) or numbers (numeric arrays). Here we will not discuss multi-dimensional arrays.
(1) When the key name is a number (value array), array_merge () does not overwrite the original value, but + merges the array to return 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 (associated array), + returns the first value as the final result, and discards the values with the same key name in the subsequent array, however, array_merge () overwrites the values with the same key name.
The following are examples:
M: Array (
[0] =>
[1] => B
)
N: Array (
[0] => c
[1] => d
)
M + n: Array (
[0] =>
[1] => B
)
The result of array_merge (m, n) is: Array (
[0] =>
[1] => B
[2] => c
[3] => d
)
M: Array (
[1] =>
[2] => B
)
N: Array (
[2] => c
[3] => d
)
M + n: Array (
[1] =>
[2] => B
[3] => d
)
The result of array_merge (m, n) is: Array (
[0] =>
[1] => B
[2] => c
[3] => d
)
M: Array (
[A] =>
[B] => B
)
N: Array (
[B] => c
[D] => d
)
M + n: Array (
[A] =>
[B] => B
[D] => d
)
The result of array_merge (m, n) is: Array (
[A] =>
[B] => c
[D] => d
)
Articles you may be interested in
- Php searches for the existence of a value in the array (in_array (), array_search (), array_key_exists ())
- PHP generates continuous numbers (letters) Array Function range () analysis, PHP lottery program function
- Differences between variables and functions in php after the static keyword is added
- Use break, continue, goto, return, and exit in multiple loops in PHP
- Php string replacement function str_replace is faster than preg_replace
- Php obtains all the files in the directory and saves the results to the array program.
- Use the PHP function memory_get_usage to obtain the current PHP memory consumption for program performance optimization.
- Performance Comparison of using in_array () foreach array_search () to find whether an array contains