php array_merge 和 array + array 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5394157/
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
What's the difference between array_merge and array + array?
提问by The Pixel Developer
A fairly simple question. What's the difference between:
一个相当简单的问题。有什么区别:
$merged = array_merge($array1, $array2);
and
和
$merged = $array1 + $array2;
?
?
采纳答案by Mike Lewis
The difference is:
区别在于:
The +
operator takes the union of the two arrays, whereas the array_merge
function takes the union BUTthe duplicate keys are overwritten.
该+
操作需要两个数组的工会,而array_merge
函数需要工会BUT的重复键被覆盖。
回答by Yehosef
Here's a simple illustrative test:
这是一个简单的说明性测试:
$ar1 = [
0 => '1-0',
'a' => '1-a',
'b' => '1-b'
];
$ar2 = [
0 => '2-0',
1 => '2-1',
'b' => '2-b',
'c' => '2-c'
];
print_r($ar1+$ar2);
print_r(array_merge($ar1,$ar2));
with the result:
结果:
Array
(
[0] => 1-0
[a] => 1-a
[b] => 1-b
[1] => 2-1
[c] => 2-c
)
Array
(
[0] => 1-0
[a] => 1-a
[b] => 2-b
[1] => 2-0
[2] => 2-1
[c] => 2-c
)
Notice that duplicate non-numeric keys will take the first value using the union operator but the later one using the array_merge.
请注意,重复的非数字键将使用联合运算符取第一个值,但使用 array_merge 取后一个值。
For numeric keys, the first value will be used with the union operator whereas the all the values will be used with the array_merge, just reindexed.
对于数字键,第一个值将与联合运算符一起使用,而所有值将与 array_merge 一起使用,只是重新索引。
I generally use union operator for associative arrays and array_merge for numeric. Of course, you can just as well use the array_merge for associative, just that the later values overwrite earlier ones.
我通常对关联数组使用联合运算符,对数字使用 array_merge。当然,您也可以使用 array_merge 进行关联,只是后面的值会覆盖前面的值。
回答by BoltClock
array_merge()
causes all numeric keys found in the input arrays to be reindexed in the resultant array. The union operator +
does not cause a reindex.
array_merge()
导致在输入数组中找到的所有数字键在结果数组中重新索引。联合运算符+
不会导致重新索引。
回答by luchaninov
Source: https://softonsofa.com/php-array_merge-vs-array_replace-vs-plus-aka-union/
来源:https: //softonsofa.com/php-array_merge-vs-array_replace-vs-plus-aka-union/
Stop using array_merge($defaults, $options):
停止使用 array_merge($defaults, $options):
function foo(array $options)
{
$options += ['foo' => 'bar'];
// ...
}
Note: array_replace function exists since PHP5.3.
注意:array_replace 函数从 PHP5.3 开始存在。
回答by Tschallacka
The + sign only takes the value from the firstoccurence of an array key.
array_merge takes the value from the lastoccurrence of an array key.
+ 号只取数组键第一次出现时的值。
array_merge 从最后一次出现的数组键中获取值。
Example:
例子:
$first = ['a'=>'one',
'b'=>'two',
'c'=>'three'];
$second = ['a'=>'fourth',
'b'=>'fifth',
'c'=>'sixth',
'3'=>'number three'];
$merged = $first + $second;
echo "<pre> plus sign merge\n";
var_dump($merged);
$merged = array_merge($first,$second);
echo "\n array_merge function merge\n";
var_dump($merged);
This outputs:
这输出:
plus sign merge
array(4) {
["a"]=>
string(3) "one"
["b"]=>
string(3) "two"
["c"]=>
string(5) "three"
[3]=>
string(12) "number three"
}array_merge function merge
array(4) {
["a"]=>
string(6) "fourth"
["b"]=>
string(5) "fifth"
["c"]=>
string(5) "sixth"
[0]=>
string(12) "number three"
}
加号合并
数组(4) {
["a"]=>
string(3) "one"
["b"]=>
string(3) "two"
["c"]=>
string(5) "three"
[3]=>
字符串(12)“数字三”
}array_merge function merge
array(4) {
["a"]=>
string(6) "fourth"
["b"]=>
string(5) "fifth"
["c"]=>
string(5) "sixth"
[0]=>
字符串(12)“数字三”
}
Interesting to note in this is that the array_merge
actally erasesthe '3' index of number three even though it's a string, because it's a number.
有趣的是,尽管它是一个字符串,但array_merge
实际上删除了数字 3 的 '3' 索引,因为它是一个数字。
So take care when merging with array_merge
arrays with numerical indexes. They might lose their keys. if they are important to you precede them with a string.
因此,在与array_merge
具有数字索引的数组合并时要小心。他们可能会丢失钥匙。如果它们对您很重要,请在它们前面加上一个字符串。
so instead of '3' => 'three'
use something like '_3' => 'three'
所以而不是'3' => 'three'
使用类似的东西'_3' => 'three'
回答by klennepette
I believe array_merge
overwrites duplicate non_numeric keys while $array1 + $array2
does not.
我相信会array_merge
覆盖重复的非数字键,而$array1 + $array2
不会。
回答by jacek.ciach
Yet another example (arrays without explicit keys; it's obvious regarding to how the operator +
and array_merge
work, but "obvious" things are simpler when seen ;))
另一个例子(没有显式键的数组;关于运算符+
和array_merge
工作的方式很明显,但是“显而易见”的事情在看到时更简单;))
$a = array('apple');
$b = array('orange', 'lemon');
echo '$a + $b = '; print_r($a + $b);
echo 'array_merge($a, $b) = '; print_r(array_merge($a, $b));
will give:
会给:
$a + $b = Array
(
[0] => apple
[1] => lemon
)
array_merge($a, $b) = Array
(
[0] => apple
[1] => orange
[2] => lemon
)
回答by Nedyalko Dyakov
So apparently if you change the order both union and merge will do the same thing
所以很明显,如果你改变顺序, union 和 merge 都会做同样的事情
$a = array('foo' => 'bar', 'x' => 'fromA');
$b = array('foo' => null, 'x' => 'fromB');
echo '$a+$b: ';
var_dump($a+$b);
echo '$b+$a: ';
var_dump($b+$a);
echo 'array_merge($a, $b): ';
var_dump(array_merge($a, $b));
echo 'array_merge($b, $a): ';
var_dump(array_merge($b, $a));
Outputs :
输出:
$a+$b: array(2) {
["foo"]=>
string(3) "bar"
["x"]=>
string(5) "fromA"
}
$b+$a: array(2) {
["foo"]=>
NULL
["x"]=>
string(5) "fromB"
}
array_merge($a, $b): array(2) {
["foo"]=>
NULL
["x"]=>
string(5) "fromB"
}
array_merge($b, $a): array(2) {
["foo"]=>
string(3) "bar"
["x"]=>
string(5) "fromA"
}
Keep in mind the order of the arrays.
请记住数组的顺序。
回答by ZalemCitizen
Please pay attention for another difference: the union (+) won't overwrite non-empty value with empty value (considering a same key), whereas array_merge will:
请注意另一个区别:联合 (+) 不会用空值覆盖非空值(考虑相同的键),而 array_merge 将:
$a = array('foo' => 'bar');
$b = array('foo' => ''); // or false or 0
print_r($a+$b);
print_r(array_merge($a, $b);
Outputs :
输出:
Array
(
[foo] => bar
)
Array
(
[foo] => 0
)