php Array_merge 与 +
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7059721/
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
Array_merge versus +
提问by Elly
When I use array_merge()
with associative arrays I get what I want, but when I use them with numerical key arrays the keys get changed.
当我array_merge()
与关联数组一起使用时,我得到了我想要的东西,但是当我将它们与数字键数组一起使用时,键会发生变化。
With +
the keys are preserved but it doesn't work with associative arrays.
随着+
该密钥将被保留,但它不与关联数组。
I don't understand how this works, can anybody explain it to me?
我不明白这是如何工作的,有人可以向我解释一下吗?
回答by Christopher Armstrong
Because both arrays are numerically-indexed, only the values in the first array will be used.
因为两个数组都是数字索引的,所以只会使用第一个数组中的值。
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.
+ 运算符返回附加到左侧数组的右侧数组;对于存在于两个数组中的键,将使用左侧数组中的元素,而忽略右侧数组中的匹配元素。
http://php.net/manual/en/language.operators.array.php
http://php.net/manual/en/language.operators.array.php
array_merge()
has slightly different behavior:
array_merge()
行为略有不同:
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个。但是,如果数组包含数字键,则后面的值不会覆盖原始值,而是会附加。带有数字键的输入数组中的值将在结果数组中使用从零开始的递增键重新编号。
回答by Gucci Koo
These two operation are totally different.
这两种操作完全不同。
array plus
- Array plus operation treats all array as assoc array.
- When key conflict during plus, left(previous) value will be kept
null + array()
will raise fatal errorarray_merge()
- array_merge() works different with index-array and assoc-array.
- If both parameters are index-array, array_merge() concat index-array values.
- If not, the index-array will to convert to values array, and then convert to assoc array.
- Now it got two assoc array and merge them together, when key conflict, right(last) value will be kept.
array_merge(null, array())
returns array() and got a warning said, parameter #1 is not an array.
数组加
- 数组加运算将所有数组视为关联数组。
- 当加号期间键冲突时,将保留左(前)值
null + array()
会引发致命错误数组合并()
- array_merge() 与 index-array 和 assoc-array 的工作方式不同。
- 如果两个参数都是索引数组,则 array_merge() 连接索引数组值。
- 如果不是,索引数组将转换为值数组,然后转换为关联数组。
- 现在它有两个关联数组并将它们合并在一起,当键冲突时,将保留正确的(最后一个)值。
array_merge(null, array())
返回 array() 并收到警告说,参数 #1 不是数组。
I post the code below to make things clear.
我发布了下面的代码以使事情清楚。
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;
}
//----------------------------------------------------------------
function is_index($a){
$keys = array_keys($a);
foreach($keys as $key) {
$i = intval($key);
if("$key"!="$i") return false;
}
return true;
}
function array_merge($a, $b){
if(is_index($a)) $a = array_values($a);
if(is_index($b)) $b = array_values($b);
$results = array();
if(is_index($a) and is_index($b)){
foreach($a as $v) $results[] = $v;
foreach($b as $v) $results[] = $v;
}
else{
foreach($a as $k=>$v) $results[$k] = $v;
foreach($b as $k=>$v) $results[$k] = $v;
}
return $results;
}