This article mainly introduces a problem that needs to be paid attention to when using the phparray_merge function. This article describes the problem that the number key names will not be merged when the array_merge array is merged. You need to pay attention when using the function. For more information, see
This article mainly introduces a problem that needs to be paid attention to when using the php array_merge function. This article describes the problem that array_merge does not merge the number key names when merging arrays. You need to pay attention when using the function. For more information, see
When using the array_merge function in php, the same key name will be overwritten, but please refer to the following code:
The Code is as follows:
$ A1 = array (1 => 'abc', 3 => 10 );
$ A2 = array (1 => 'efg', 3 => 20 );
Print_r (array_merge ($ a1, $ a2 ));
What will be output? We expected:
The Code is as follows:
Array
(
[1] => efg
[3] => 20
)
Actually, the output is:
The Code is as follows:
Array
(
[0] => abc
[1] => 10
[2] => efg
[3] => 20
)
Not only is it not overwritten, but the number key is re-indexed continuously.
At first, I thought this was a bug. Later I flipped through the php manual.
「 If the input array contains the same string key name, the value after the key name overwrites the previous one. However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.
If only one array is assigned and the array is indexed by number, the key name is re-indexed continuously .」