博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
合并两个数组的两种方式的异同
阅读量:4691 次
发布时间:2019-06-09

本文共 1025 字,大约阅读时间需要 3 分钟。

合并数组的两种方式:$a+$b , array_merge($a,$b)

合并索引数组使用操作符+,有重复的索引会被丢弃;使用array_merge() 重复索引会被重置,如下面这种情况:

1 php > $a = array();   2  3 php > $b = array(1=>'data');   4  5 php > $c = $a+$b;   6  7 php > $d = array_merge($a,$b);   8  9 php > var_dump($c,$d);  10 11 //输出结果12 13 array(1) {  [1] =>  string(4) "data" }  14 15 array(1) {  [0] =>  string(4) "data" }

 

所以,当使用+来合并两个索引数组,值可能会被丢弃,而array_merge就不会,会把重复的索引重置:

1 php > $a = array(1=>'one',2=>'two',3=>'three');   2 php > $b = array(3=>'three',4=>'four',5=>'five');   3 php > $c = $a+$b;   4 php > $d = array_merge($a,$b);   5 php > var_dump($c,$d);   6 array(5) {   7   [1] =>  string(3) "one"   8   [2] =>  string(3) "two"   9   [3] =>  string(5) "three"  10   [4] =>  string(4) "four"  11   [5] =>  string(4) "five"  12 }  13 14 array(6) {  15   [0] =>  string(3) "one"  16   [1] =>  string(3) "two"  17   [2] =>  string(5) "three"  18   [3] =>  string(5) "three"  19   [4] =>  string(4) "four"  20   [5] =>  string(4) "five"  21 }

 

转载于:https://www.cnblogs.com/hellodp/p/5537480.html

你可能感兴趣的文章
.net开源CMS
查看>>
JdbcTemplate
查看>>
第一次使用maven记录
查看>>
SharePoint服务器端对象模型 之 使用CAML进展数据查询
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
postgressql数据库中limit offset使用
查看>>