时间:2021-07-01 10:21:17 帮助过:22人阅读
其中数组相加以及array_merge让笔者踩了个小坑。
一、请看demo1:
public function action_test54(){ $a = array( '0'=>1567, '1'=>1568, '2'=>1569, '3'=>1570, ); $b = array( '1'=>1571, '2'=>1572, '3'=>1573, '4'=>1574 ); d($a + $b); dd(array_merge($a,$b)); }
demo1输出:
array(5) ( 0 => integer 1567 1 => integer 1568 2 => integer 1569 3 => integer 1570 4 => integer 1574 )
array(8) ( 0 => integer 1567 1 => integer 1568 2 => integer 1569 3 => integer 1570 4 => integer 1571 5 => integer 1572 6 => integer 1573 7 => integer 1574 )
结论:
$a + $b 增量覆盖
array_merge($a,$b) 合并
二、Demo2
public function action_test54(){ $a = array( 'a'=>1567, 'b'=>1568, 'c'=>1569, 'd'=>1570, ); $b = array( 'b'=>1571, 'c'=>1572, 'd'=>1573, 'e'=>1574 ); d($a + $b); dd(array_merge($a,$b)); }
array(5) ( "a" => integer 1567 "b" => integer 1568 "c" => integer 1569 "d" => integer 1570 "e" => integer 1574 )
array(5) ( "a" => integer 1567 "b" => integer 1571 "c" => integer 1572 "d" => integer 1573 "e" => integer 1574 )
结论:
$a + $b 增量覆盖
array_merger($a,$b) $b优先替换掉$a
以上就介绍了PHP 数组相加和array_merge的区别,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。