解析php中array_merge与array+array的区别
编程学习 2021-07-05 09:49www.dzhlxh.cn编程入门
本篇文章是对php中array_merge与array+array的区别进行了详细的分析介绍,需要的朋友参考下
array_merge是丢弃原来的数字的key,而保留字符串形式的key,然后组成一个新的数组,不管键名是否一样,都不合并,除非键名和value同时一样并且还必须是字符串形式的key才合并。而array+array就是不管你是什么情况,它都只会先把前面的数组的数据先放到新生成的数组中,之后再看第二个array是否比第一个数组个数多,多的就添加进来,它只数个数,但是这种情况它会添加进来:$a = array('d'=>'aass','e'=>'adsdfd','asd'=>'asdsdd','ddfg'=>'dssdf');
$b = array('d'=>'adddd','adsdfd','asdfsddddd','d'=>'aass');
$d = $a + $b;
$e = array_merge($a,$b);
var_dump($d);
var_dump($e);
打印:
array
'd' => string 'aass' (length=4)
'e' => string 'adsdfd' (length=6)
'asd' => string 'asdsdd' (length=6)
'ddfg' => string 'dssdf' (length=5)
0 => string 'adsdfd' (length=6)
1 => string 'asdfsddddd' (length=10)
array
'd' => string 'aass' (length=4)
'e' => string 'adsdfd' (length=6)
'asd' => string 'asdsdd' (length=6)
'ddfg' => string 'dssdf' (length=5)
0 => string 'adsdfd' (length=6)
1 => string 'asdfsddddd' (length=10)
代码如下:
$b = array('d'=>'adddd','adsdfd','asdfsddddd','d'=>'aass');
$d = $a + $b;
$e = array_merge($a,$b);
var_dump($d);
var_dump($e);
打印:
array
'd' => string 'aass' (length=4)
'e' => string 'adsdfd' (length=6)
'asd' => string 'asdsdd' (length=6)
'ddfg' => string 'dssdf' (length=5)
0 => string 'adsdfd' (length=6)
1 => string 'asdfsddddd' (length=10)
array
'd' => string 'aass' (length=4)
'e' => string 'adsdfd' (length=6)
'asd' => string 'asdsdd' (length=6)
'ddfg' => string 'dssdf' (length=5)
0 => string 'adsdfd' (length=6)
1 => string 'asdfsddddd' (length=10)
上一篇:浅析php中jsonp的跨域实例
下一篇:邮箱正则表达式实现代码(针对php)