array_mergeの履歴
忘れやすいのでメモ。
array_mergeは重複キーを上書きする。
+演算子は重複キーを上書きしない。重複した数字添え字の場合は追記する形になる
<?php $x = array('aaa' => 1, 'bbb' => 2, 'ccc' => 3); $y = array('ccc' => 5, 'ddd' => 6); print_r(array_merge($x, $y)); print_r($x + $y); $x = array('a', 'b', 'c', 'd'); $y = array('b', 'c'); print_r(array_merge($x, $y)); print_r($x + $y);
Array ( [aaa] => 1 [bbb] => 2 [ccc] => 5 [ddd] => 6 ) Array ( [aaa] => 1 [bbb] => 2 [ccc] => 3 [ddd] => 6 ) Array ( [0] => a [1] => b [2] => c [3] => d [4] => b [5] => c ) Array ( [0] => a [1] => b [2] => c [3] => d )