Home >Backend Development >PHP Tutorial >There is a problem that needs to be paid attention to when using the php array_merge function, phparray_merge_PHP tutorial

There is a problem that needs to be paid attention to when using the php array_merge function, phparray_merge_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:59:04785browse

There is a problem that needs to be paid attention to when using the php array_merge function. phparray_merge

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:
Copy 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? What we expected was:
Copy code The code is as follows:
Array
(
[1] => efg
[3] => 20
)

The actual output is:

Copy code The code is as follows:
Array
(
[0] => abc
[1] => 10
[2] => efg
[3] => 20
)

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. 》

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/976035.htmlTechArticleThere is a problem that needs to be paid attention to when using the php array_merge function. phparray_merge When using the array_merge function in the php language, the same key name is assumed will be overwritten, but please see the following code: Copy code...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn