PHP中数组的+运算符?

+ operator for array in PHP?

1
2
3
$test = array('hi');
$test += array('test','oh');
var_dump($test);

对于PHP中的数组,+意味着什么?


从PHP语言操作手册中引用

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

所以如果你这样做

1
2
3
4
$array1 = ['one',   'two',          'foo' => 'bar'];
$array2 = ['three', 'four', 'five', 'foo' => 'baz'];

print_r($array1 + $array2);

你会得到

1
2
3
4
5
6
7
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)
)

因此,+的逻辑相当于以下代码片段:

1
2
3
4
5
6
7
$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()组合阵列的方式不同:

1
print_r(array_merge($array1, $array2));

会给你

1
2
3
4
5
6
7
8
9
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
)

有关更多示例,请参见链接页。


我发现使用它的最佳示例是在配置数组中。

1
2
3
4
$user_vars = array("username"=>"John Doe");
$default_vars = array("username"=>"Unknown","email"=>"[email protected]");

$config = $user_vars + $default_vars;

顾名思义,$default_vars是默认值的数组。$user_vars数组将覆盖$default_vars中定义的值。$user_vars中任何缺少的值现在都是$default_vars中的默认变量。

这将使cx1〔8〕成为:

1
2
3
4
Array(2){
   "username" =>"John Doe",
   "email" =>"[email protected]"
}

希望这有帮助!


此运算符接受两个数组的并集(与"数组合并"相同,但"数组合并"的重复键将被覆盖)。

在这里可以找到数组运算符的文档。


如果需要保留数字键或不想释放任何内容,请谨慎使用数字键

1
2
$a = array(2 =>"a2", 4 =>"a4", 5 =>"a5");
$b = array(1 =>"b1", 3 =>"b3", 4 =>"b4");

联盟

1
2
3
4
5
6
7
8
9
print_r($a+$b);
Array
(
    [2] => a2
    [4] => a4
    [5] => a5
    [1] => b1
    [3] => b3
)

合并

1
2
3
4
5
6
7
8
9
10
print_r(array_merge($a, $b));
Array
(
    [0] => a2
    [1] => a4
    [2] => a5
    [3] => b1
    [4] => b3
    [5] => b4
)

+运算符产生的结果与array_replace()相同。但是,由于运算符参数是相反的,因此结果数组的顺序也可能不同。

在此页面的另一个示例上展开:

1
2
3
4
5
$array1 = array('one', 'two', 'foo' => 'bar');
$array2 = array('three', 'four', 'five', 'foo' => 'baz');

print_r($array1 + $array2);
print_r(array_replace($array2, $array1)); //note reversed argument order

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Array
(
    [0] => one   // preserved from $array1
    [1] => two   // preserved from $array1
    [foo] => bar // preserved from $array1
    [2] => five  // added from $array2
)
Array
(
    [0] => one   // preserved from $array1
    [1] => two   // preserved from $array1
    [2] => five  // added from $array2
    [foo] => bar // preserved from $array1
)


  • Array plus operation treats all array as assoc array.
  • When key conflict during plus, left(previous) value will be kept
  • 我发布下面的代码以使事情清楚。

    $a + $b = array_plus($a, $b)

    1
    2
    3
    4
    5
    6
    function array_plus($a, $b){
        $results = array();
        foreach($a as $k=>$v) if(!isset($results[$k]))$results[$k] = $v;
        foreach($b as $k=>$v) if(!isset($results[$k]))$results[$k] = $v;
        return $results;
    }


    它将把新数组追加到前一个数组。


    1
    2
    3
    4
    $var1 ="example";
    $var2 ="test";
    $output = array_merge((array)$var1,(array)$var2);
    print_r($output);

    数组([0]=>示例[1]=>测试)