数组操作我们经常使用 "+" 与 array_merge,但是要注意一些细节
对于 '+'
<?php
$a = ['a','b'];
$b = ['c','d','e'];
$c = $a + $b; //['a','b','e']
?>
注意:对于 '+' 而言, 前面数组会覆盖相同键的后面数组
对于 'array_merge'
<?php
//e.g.1
$a = ['a','b'];
$b = ['c','d','e'];
$c = array_merge($a , $b);//['a','b','c','d','e']
//e.g.2
$a = ['a'=> 1,'b'=> 4];
$b = ['a' => 2,'d'=> 6];
$c = array_merge($a , $b);//["a" => 2,"b" => 4,"d" => 6]
?>
注意:array_merge对于字符串键值来说,后面的数组会覆盖前面的;array_merge对于数字键值来说,后面的数组会和前面的进行合并,并且重新索引
Comment here is closed