Array_merge is the most reliable function in php. When we use the array_merge function to merge below, we mainly use the array_merge function to merge the two numeric key arrays. The key values of the array are processed as numbers.
You need to understand a basic knowledge point first
You can use + or array_merge to combine two arrays in PHP, but there are still differences between them, and if you don’t understand these differences clearly, it will be fatal in the project!
The main difference is that if the same key name appears in two or more arrays, the key name is divided into a string or a number. Please note
1) When the key name is a number, the value after array_merge() will not overwrite the original value, but will be appended to the end. However, the +merged array will return the first value as the final result, and the Those values in the subsequent arrays with the same key names are "discarded" (not overwritten)
2) When the key name is a string, array_merge() will overwrite the previous value with the same key name, but + will still return the first value as the final result, and the subsequent arrays with the same key name will be returned Those values are "discarded" (not overwritten).
The code is as follows | |
$a = array('a', 'b'); //Output: array array ++++++++++++++++++++++++++++++++++++++++++ //输出: array array ++++++++++++++++++++++++++++++++++++++++++ $a = array('a', 'b'); //输出: array ++++++++++++++++++++++++++++++++++++++++++ $a = array( 输出: array array |
Okay, that’s enough. Let’s take a look at the value of merging two numeric key arrays
The code is as follows | |
/** $arr1 = array('aa', 'bb', 'cc'); $arr = new_array_merge($arr1, $arr2); Output: Array |