当前位置:网站首页>Array in PHP_ The pit of merge
Array in PHP_ The pit of merge
2022-07-24 15:48:00 【aben_ sky】
PHP The official document is right array_merge The definition of
array_merge( array $array1[, array $...] ) : array
array_merge() Combine the cells of one or more arrays , The values in an array are appended to the back of the previous array . Returns an array as a result .
If the input array has the same string key name , Then the value after the key name will overwrite the previous value . However , If the array contains a numeric key name , The latter value will not overwrite the original value , It's attached to the back .
If only one array is given and the array is digitally indexed , The key name is re indexed in a continuous manner .
Description of pit
Look at the bold part of the document above , If you believe , And just believe what the document says , Then you're wrong .
See the example given in the document :
$array1 = array();
$array2 = array(1 => "data");
$result = array_merge($array1, $array2);
var_dump($result);
The result is :
Array
(
[0] => data
)
Um. , Yes , There is no problem with the example given in the document , There's really no problem .
however , If both arrays are not empty , And there are numerical subscripts , It will also re index the numerical subscript
$arr = [
11 => 'a',
12 => 'b'
];
$arr2 = [
'a' => 'a1',
14 => 'b1'
];
var_dump(array_merge($arr, $arr2));
The result is :
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
["a"]=>
string(2) "a1"
[2]=>
string(2) "b1"
}
The numerical subscripts have been reset !
so, In this case, only + To deal with it :
$result = $arr + $arr2;
边栏推荐
- [tf.keras]: a problem encountered in upgrading the version from 1.x to 2.x: invalidargumenterror: cannot assign a device for operation embedding_
- Introduction to kettle messy notes
- [SWT] user defined data table
- 什么是防火墙?防火墙能发挥什么样的作用?
- Lsyncd 实时同步
- vscode常用快捷键
- 自适应设计和响应式设计
- 上课作业(6)——#575. 单词的划分(word)
- Feign for 20 minutes every day
- Kubernetes static storage and dynamic storage
猜你喜欢

Error: pidfile (celerybeat.pid) already exists when celery starts beat

Dynamics crm: mailbox configuration (III) - configure email server profiles and mailboxes
![[TA frost wolf \u may - hundred people plan] Figure 3.4 introduction to delayed rendering pipeline](/img/8a/0acf8f5bee7d3b92f7c5b0453fb9d5.png)
[TA frost wolf \u may - hundred people plan] Figure 3.4 introduction to delayed rendering pipeline
![[SWT] scrolling container to realize commodity list style](/img/84/07e7c794aaef3fb64f173b50150b21.png)
[SWT] scrolling container to realize commodity list style

Force button 31. Next arrangement -- double finger needling

Withdrawal of IPO application, Yunzhou intelligent "tour" of unmanned boat enterprise fails to enter the science and Technology Innovation Board

From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?

Arduino ide esp32 firmware installation and upgrade tutorial

What is a firewall? What role can firewalls play?

2.19 haas506 2.0开发教程 - bluetooth - 蓝牙通信(仅支持2.2以上版本)
随机推荐
Kubernetes version docking object storage
mysql源码分析——索引的数据结构
公钥私钥传输,以及对CA证书的理解
Leetcode 223. rectangular area
Database learning – select (multi table joint query) [easy to understand]
Error: pidfile (celerybeat.pid) already exists when celery starts beat
Multus of kubernetes multi network card scheme_ CNI deployment and basic use
Feign for 20 minutes every day
Memorythrashing: Tiktok live broadcast to solve memory dithering practice
Research on stability of time-delay systems based on Lambert function
Dynamics crm: sharing records for users and teams
简化理解:发布订阅
LaneATT
Knowledge points of MySQL (12)
Dynamics 365: explain virtual entity from 0 to 1
三、集合基础——ArrayList集合与简单学生管理系统
【SWT】自定义数据表格
You are only one SQL statement away from the tdengine Developer Conference!
JUC源码学习笔记3——AQS等待队列和CyclicBarrier,BlockingQueue
After taking aiyouteng's medicine, Naifei's condition improved