Home >Backend Development >PHP Tutorial >An issue you need to pay attention to when using the php array_merge function_PHP Tutorial
This article mainly introduces an issue you need to pay attention to when using the php array_merge function. This article explains that array_merge will not merge numbers when merging arrays. Everyone needs to pay attention to the problem of key names when using it. Friends in need can refer to it
When using the array_merge function in PHP language, it is thought that the same key name will be overwritten, but please see the following code:
The code is as follows:
What will be output? What we expected was:
The code is as follows:
The actual output is:
The code is as follows:
Not only was it not overwritten, but the numeric keys were re-indexed consecutively.
At first I thought this was a bug, then I read the php manual http://php.net/manual/zh/function.array-merge.php
「If the input array has the same string key name, the value after the key name will overwrite the previous value. However, if the array contains a numeric key name, the subsequent value will not overwrite the original value. Instead, append it to
.
If only an array is given and the array is numerically indexed, the key names are re-indexed consecutively. 》