PHP中两个数组合并可以使用+或者array_merge,但之间还是有区别的,而且这些区别如果了解不清楚项目中会要命的!
主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意
复制代码 代码如下:
$a = array('a','b');
$b = array('c', 'd');
$c = $a + $b;
var_dump($a);
var_dump(array_merge($a, $b));
$a = array(0 => 'a', 1 => 'b');
$b = array(0 => 'c', 1 => 'b');
$c = $a + $b;
var_dump($c);
var_dump(array_merge($a, $b));
$a = array('a', 'b');
$b = array('0' => 'c', 1 => 'b');
$c = $a + $b;
var_dump($c);
var_dump(array_merge($a, $b));
$a = array(0 => 'a', 1 => 'b');
$b = array('0' => 'c', '1' => 'b');
$c = $a + $b;
var_dump($c);
var_dump(array_merge($a, $b));
复制代码 代码如下:
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'd' (length=1)
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'b' (length=1)
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'b' (length=1)
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'b' (length=1)