解析php中array_merge与array+array的区别_php技巧
程序员文章站
2022-04-27 08:30:09
...
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)
上一篇: ASP SQL防注入的方法_jquery