慕仰8121524
引用PHP语言操作员手册+运算符返回附加到左侧数组的右侧数组; 对于存在于两个数组中的键,将使用左侧数组中的元素,并且将忽略右侧数组中的匹配元素。所以,如果你这样做$array1 = ['one', 'two', 'foo' => 'bar'];$array2 = ['three', 'four', 'five', 'foo' => 'baz']; print_r($array1 + $array2);你会得到Array(
[0] => one // preserved from $array1 (left-hand array)
[1] => two // preserved from $array1 (left-hand array)
[foo] => bar // preserved from $array1 (left-hand array)
[2] => five // added from $array2 (right-hand array))所以逻辑+相当于以下片段:$union = $array1;foreach ($array2 as $key => $value) {
if (false === array_key_exists($key, $union)) {
$union[$key] = $value;
}}如果您对C级实现的详细信息感兴趣php-src / Zend / zend_operators.c注意,这+与array_merge()组合数组的方式不同:print_r(array_merge($array1, $array2));会给你的Array(
[0] => one // preserved from $array1
[1] => two // preserved from $array1
[foo] => baz // overwritten from $array2
[2] => three // appended from $array2
[3] => four // appended from $array2
[4] => five // appended from $array2)有关更多示例,请参见链接页面