Home >php教程 >php手册 >解析php中array_merge与array+array的区别

解析php中array_merge与array+array的区别

WBOY
WBOYOriginal
2016-06-06 20:29:551277browse

本篇文章是对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)
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