我有一些代码,似乎使用+=合并来自两个数组的数据,但它不包括元素中的所有元素。它是如何工作的?

例子:

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

输出:

array(2) {
  [0]=>
  string(2) "hi"
  [1]=>
  string(2) "oh"
}

在PHP数组中使用+是什么意思?


引用自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
)

更多示例请参见链接页面。


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


该运算符接受两个数组的并集(与array_merge相同,只是使用array_merge时重复的键会被覆盖)。

数组操作符的文档可以在这里找到。


我发现最好的例子是在配置数组中。

$user_vars = array("username"=>"John Doe");
$default_vars = array("username"=>"Unknown", "email"=>"no-reply@domain.com");

$config = $user_vars + $default_vars;

如它所示,$default_vars是默认值的数组。 $user_vars数组将覆盖$default_vars中定义的值。 $user_vars中的任何缺失值现在都是$default_vars中的默认值。

这将把print_r打印为:

Array(2){
    "username" => "John Doe",
    "email" => "no-reply@domain.com"
}

我希望这能有所帮助!


$var1 = "example";
$var2 = "test";
$output = array_merge((array)$var1,(array)$var2);
print_r($output);

数组([0]=> example [1] => test)


小心使用数字键,如果它们应该保留,或者如果你不想丢失任何东西

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

联盟

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

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

+操作符产生的结果与array_replace()相同。但是,由于运算符参数颠倒了,结果数组的顺序也可能不同。

从本页展开另一个例子:

$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

输出:

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
)

数组加操作将所有数组视为关联数组。 当加号期间键冲突时,左边(上一个)值将被保留

我在下面发布代码以使事情更清楚。

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

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;
}

从https://softonsofa.com/php-array_merge-vs-array_replace-vs-plus-aka-union/

也就是说,我们可以认为+运算符有点多余,因为array_replace函数也可以实现同样的效果。 然而,在有些情况下,它派上用场:假设你有一个$options数组被传递给一个函数/方法,也有默认值用作备用:

// we could do it like this
function foo(array $options)
{
   $defaults = ['foo' => 'bar'];
   
   $options = array_replace($defaults, $options);
 
   // ...
}
 
// but + here might be way better:
function foo(array $options)
{
   $options += ['foo' => 'bar'];
 
   // ...
}