当前位置:网站首页>PHP中array_merge的坑
PHP中array_merge的坑
2022-07-24 15:41:00 【aben_sky】
PHP官方文档对array_merge的定义
array_merge( array $array1[, array $...] ) : array
array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。
如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。
对坑的说明
看上面的文档的粗体字部分, 如果你信了, 并且只是相信文档说的这一种情况, 那你就错了.
看文档给的例子:
$array1 = array();
$array2 = array(1 => "data");
$result = array_merge($array1, $array2);
var_dump($result);
结果是:
Array
(
[0] => data
)
嗯, 对, 文档给的这个例子没有问题, 确实没有任何问题.
但是, 如果两个数组都不为空, 且有数字下标, 它一样会将数字下标重新索引
$arr = [
11 => 'a',
12 => 'b'
];
$arr2 = [
'a' => 'a1',
14 => 'b1'
];
var_dump(array_merge($arr, $arr2));
结果是:
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
["a"]=>
string(2) "a1"
[2]=>
string(2) "b1"
}
数字下标都被重置了!
so, 这种情况下只能使用 + 来处理:
$result = $arr + $arr2;
边栏推荐
- 自适应设计和响应式设计
- 有了这个机器学习画图神器,论文、博客都可以事半功倍了!
- [TA frost wolf \u may - hundred people plan] Figure 3.4 introduction to delayed rendering pipeline
- Experience summary of slow SQL problems
- 华为无线设备配置WAPI-证书安全策略
- Fine tune layoutlm V3 for bill data processing and content recognition
- G026-db-gs-ins-03 openeuler deployment opengauss (1 active and 2 standby or multiple standby)
- Leetcode 231. 2 的幂
- 2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 3 running team robot (finished)
- MySQL之知识点(十二)
猜你喜欢

PyTorch的自动求导

【SWT】自定义数据表格

You can't just focus on flex layout and elaborate animation to explain all flex layout methods! Easy to understand dry goods tutorial

Hard core innovation that database needs to care about in the future
![Dynamics crm: [problem solved]cannot open SQL encryption symmetric key because symmetric key password](/img/ae/125fcb16c9d85714c7bbd1255d1d18.png)
Dynamics crm: [problem solved]cannot open SQL encryption symmetric key because symmetric key password

What is a firewall? What role can firewalls play?

Automatic derivation of pytorch

Leetcode 220. 存在重复元素 III

Arduino IDE ESP32固件安装和升级教程

Research on stability of time-delay systems based on Lambert function
随机推荐
yolov3 训练自己的数据集
Kubernetes version docking object storage
Leetcode 223. 矩形面积
报错【项目报错】
kubernetes多网卡方案之Multus_CNI部署和基本使用
Feign for 20 minutes every day
Netease email (126/163): authorization code acquisition strategy
G026-db-gs-ins-03 openeuler deployment opengauss (1 active and 2 standby or multiple standby)
Reentrantlock reentrant lock
R language ggplot2 visualization: ggplot2 visualization basic scatter plot, through in theme_ Specify the parameter base in BW_ Size to change the size of axis labels and control the size of gridlines
Automatic derivation of pytorch
Is it safe for Huatai Securities to open a mobile account and will it be leaked
4279. 笛卡尔树
Class assignment (6) - 575. Word division (word)
[Luogu] p1908 reverse sequence pair
Small list of iptables common commands
[SWT] scrolling container to realize commodity list style
Dynamics crm: sharing records for users and teams
Leetcode 220. 存在重复元素 III
Arduino IDE ESP32固件安装和升级教程