+ 运算符在 PHP 中的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2140090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
+ operator for array in PHP?
提问by user198729
$test = array('hi');
$test += array('test','oh');
var_dump($test);
What does +mean for array in PHP?
+PHP中的数组是什么意思?
回答by Gordon
Quoting from the PHP Manual on Language Operators
引自PHP Manual on Language Operators
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.
+ 运算符返回附加到左侧数组的右侧数组;对于存在于两个数组中的键,将使用左侧数组中的元素,而忽略右侧数组中的匹配元素。
So if you do
所以如果你这样做
$array1 = ['one', 'two', 'foo' => 'bar'];
$array2 = ['three', 'four', 'five', 'foo' => 'baz'];
print_r($array1 + $array2);
You will get
你会得到
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)
)
So the logic of +is equivalent to the following snippet:
所以 的逻辑+相当于下面的代码片段:
$union = $array1;
foreach ($array2 as $key => $value) {
if (false === array_key_exists($key, $union)) {
$union[$key] = $value;
}
}
If you are interested in the details of the C-level implementation head to
如果您对 C 级实现的细节感兴趣,请前往
- php-src/Zend/zend_operators.c
- php-src/Zend/ zend_operators.c
Note, that +is different from how array_merge()would combine the arrays:
请注意,这+与array_merge()组合数组的方式不同:
print_r(array_merge($array1, $array2));
would give you
会给你
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
)
See linked pages for more examples.
有关更多示例,请参阅链接页面。
回答by Frank de Jonge
The best example I found for using this is in a config array.
我发现使用它的最佳示例是在配置数组中。
$user_vars = array("username"=>"John Doe");
$default_vars = array("username"=>"Unknown", "email"=>"[email protected]");
$config = $user_vars + $default_vars;
The $default_vars, as it suggests, is the array for default values.
The $user_varsarray will overwrite the values defined in $default_vars.
Any missing values in $user_varsare now the defaults vars from $default_vars.
的$default_vars,因为它表明,为默认值的数组。该$user_vars数组将覆盖 中定义的值$default_vars。中的任何缺失值$user_vars现在都是$default_vars.
This would print_ras:
这将print_r作为:
Array(2){
"username" => "John Doe",
"email" => "[email protected]"
}
I hope this helps!
我希望这有帮助!
回答by Peter Smit
回答by dcaillibaud
Carefull with numeric keys, if they should be preserved or if you don't want to loose anything
小心使用数字键,如果它们应该被保留或者如果你不想丢失任何东西
$a = array(2 => "a2", 4 => "a4", 5 => "a5");
$b = array(1 => "b1", 3 => "b3", 4 => "b4");
union
联盟
print_r($a+$b);
Array
(
[2] => a2
[4] => a4
[5] => a5
[1] => b1
[3] => b3
)
merge
合并
print_r(array_merge($a, $b));
Array
(
[0] => a2
[1] => a4
[2] => a5
[3] => b1
[4] => b3
[5] => b4
)
回答by Tamlyn
The +operator produces the same results as array_replace(). However since the operator arguments are reversed, the ordering of the resulting array may also be different.
该+运算符产生与array_replace()相同的结果。但是,由于运算符参数颠倒,结果数组的顺序也可能不同。
Expanding on another example from this page:
扩展此页面的另一个示例:
$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
outputs:
输出:
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
)
回答by Gucci Koo
- Array plus operation treats all array as assoc array.
- When key conflict during plus, left(previous) value will be kept
- 数组加运算将所有数组视为关联数组。
- 当加号期间键冲突时,将保留左(前)值
I post the code below to make things clear.
我发布了下面的代码以使事情清楚。
$a + $b = array_plus($a, $b)
$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;
}
回答by SorcyCat
It will append the new array to the previous.
它将新数组附加到前一个数组。
回答by Henning
$var1 = "example";
$var2 = "test";
$output = array_merge((array)$var1,(array)$var2);
print_r($output);
Array ( [0] => example [1] => test )
数组( [0] => 示例 [1] => 测试)

