一般PHP中合并两个数组都会使用array_merge()
$data1 = ['111' => 'aaa', '222' => 'bbb', '333' => 'ccc'];
$data2 = ['444' => 'ddd', '555' => 'eee', '666' => 'fff'];
$data = array_merge($data1, $data2);
得到结果
array(6) {
[0]=>
string(3) "aaa"
[1]=>
string(3) "bbb"
[2]=>
string(3) "ccc"
[3]=>
string(3) "ddd"
[4]=>
string(3) "eee"
[5]=>
string(3) "fff"
}
可以看到使用array_merge()会重置key值,如果key值对我们有用,不想重置,可以使用"+"合并数组。
$data1 = ['111' => 'aaa', '222' => 'bbb', '333' => 'ccc'];
$data2 = ['444' => 'ddd', '555' => 'eee', '666' => 'fff'];
$data = $data1 + $data2;
var_dump($data);
得到结果
array(6) {
[111]=>
string(3) "aaa"
[222]=>
string(3) "bbb"
[333]=>
string(3) "ccc"
[444]=>
string(3) "ddd"
[555]=>
string(3) "eee"
[666]=>
string(3) "fff"
}