+ PHP中数组的操作符?

tzxcd3kk  于 2022-12-28  发布在  PHP
关注(0)|答案(9)|浏览(153)
$test = array('hi');
$test += array('test','oh');
var_dump($test);

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

eufgjt7s

eufgjt7s1#

引用PHP Manual on Language Operators
运算符返回追加到左侧数组的右侧数组;对于存在于两个数组中的关键字,将使用左侧数组中的元素,而忽略右侧数组中的匹配元素。
所以如果你这么做了

$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/文件名

注意,+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
)

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

ev7lccsx

ev7lccsx2#

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

$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"
}

希望这能有所帮助!

2izufjch

2izufjch3#

此运算符取两个数组的并集(与array_merge相同,不同之处在于使用array_merge会覆盖重复的键)。
数组运算符的文档可在here中找到。

t3psigkw

t3psigkw4#

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

$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
)
fruv7luv

fruv7luv5#

+运算符生成的结果与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
)
jljoyd4f

jljoyd4f6#

1.数组加操作将所有数组视为关联数组。
1.当加号期间发生键冲突时,将保留左(前)值
我张贴下面的代码,使事情清楚.
第一个月

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

iq3niunx7#

来自https://softonsofa.com/php-array_merge-vs-array_replace-vs-plus-aka-union/ x1c 0d1x
也就是说,我们可以认为+操作符有点多余,因为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'];
 
   // ...
}
siv3szwd

siv3szwd8#

它会将新数组追加到以前的数组中。

jfgube3f

jfgube3f9#

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

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

相关问题