PHP數組處理強大的功能導致了各種資料處理函數。
其中數組相加以及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輸出:
<small>array</small>(5) ( 0 => <small>integer</small> 1567 1 => <small>integer</small> 1568 2 => <small>integer</small> 1569 3 => <small>integer</small> 1570 4 => <small>integer</small> 1574 )
<small>array</small>(8) ( 0 => <small>integer</small> 1567 1 => <small>integer</small> 1568 2 => <small>integer</small> 1569 3 => <small>integer</small> 1570 4 => <small>integer</small> 1571 5 => <small>integer</small> 1572 6 => <small>integer</small> 1573 7 => <small>integer</small> 1574 )
結論:
$a + $b
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)); }Demo2輸出:
<small>array</small>(5) ( "a" => <small>integer</small> 1567 "b" => <small>integer</small> 1568 "c" => <small>integer</small> 1569 "d" => <small>integer</small> 1570 "e" => <small>integer</small> 1574 )
<small>array</small>(5) ( "a" => <small>integer</small> 1567 "b" => <small>integer</small> 1571 "c" => <small>integer</small> 1572 "d" => <small>integer</small> 1573 "e" => <small>integer</small> 1574 )結論:$a + $b 增量覆蓋
array_merger($a,$b) $b優先替換掉$a
以上就介紹了PHP 數組相加和array_merge的區別,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。