Home >Backend Development >PHP Tutorial >Array_merge program to merge two arrays in PHP_PHP tutorial
In php, we use the array_merge() function to merge data. The array_merge() function merges two or more arrays into one array. If there are duplicate key names, the key value of the key will be the value corresponding to the last key name (the later one will overwrite the previous one). If the array is numerically indexed, the key names are re-indexed consecutively.
The code is as follows | Copy code |
The results are as follows: The first situation 1) Key name |
is a number, array_merge() will not overwrite the original value, but +merging the array will return the first value as the final result, and "discard" those values in the subsequent arrays with the same key name. Remove (not cover)
2) When the key name is a character, + still returns the first value as the final result, and "discards" those values in the subsequent arrays with the same key name, but array_merge() will overwrite them at this time The value of the same key name before
Note: If only an array is input to the array_merge() function, and the keys are integers, the function will return a new array with integer keys, whose keys are re-indexed starting with 0