PHP:将数组相加
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2811798/
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
PHP: Adding arrays together
提问by Svish
Could someone help me explain this? I have two snippets of code, one works as I expect, but the other does not.
有人可以帮我解释一下吗?我有两个代码片段,一个按我的预期工作,但另一个没有。
This works
这有效
$a = array('a' => 1, 'b' => 2);
$b = array('c' => 3);
$c = $a + $b;
print_r($c);
// Output
Array
(
[a] => 1
[b] => 2
[c] => 3
)
This does not
这不
$a = array('a', 'b');
$b = array('c');
$c = $a + $b;
print_r($c);
// Output
Array
(
[0] => a
[1] => b
)
What is going on here?? Why doesn't the second version also add the two arrays together? What have I misunderstood? What should I be doing instead? Or is it a bug in PHP?
这里发生了什么??为什么第二个版本不也将两个数组加在一起?我误解了什么?我应该怎么做?或者它是PHP中的一个错误?
采纳答案by jdcantrell
This is documented and correct: http://us3.php.net/manual/en/language.operators.array.php
这是记录在案且正确的:http: //us3.php.net/manual/en/language.operators.array.php
The + operator appends elements of remaining keys from the right handed array to the left handed, whereas duplicated keys are NOT overwritten.
+ 运算符将右手数组中剩余键的元素附加到左手,而重复的键不会被覆盖。
So I guess it's not a bug in php and what is suppose happen. I hadn't noticed this before either.
所以我想这不是 php 中的错误,而是假设会发生什么。我之前也没有注意到这一点。
回答by acm
to be short, this works because if you print_r both $a and $b you have:
简而言之,这是有效的,因为如果你同时打印 $a 和 $b 你有:
Array
(
[a] => 1
[b] => 2
)
and
和
Array
(
[c] => 3
)
as you can see all elements have different keys...
如您所见,所有元素都有不同的键...
as for the second example arrays, if you print $a and $b you have:
至于第二个示例数组,如果你打印 $a 和 $b 你有:
Array
(
[0] => a
[1] => b
)
and
和
Array
(
[0] => c
)
and that 0 key for both 'a' and 'c' is the issue here, the elements of second array with same keys are discarded... if you do:
'a' 和 'c' 的 0 键是这里的问题,具有相同键的第二个数组的元素被丢弃......如果你这样做:
$c = $b + $a; // instead of $c = $a + $b;
the result will be:
结果将是:
Array
(
[0] => c
[1] => b
)
回答by Andrew Hare
To add two non-associative arrays you need to use the array_mergefunction:
要添加两个非关联数组,您需要使用该array_merge函数:
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
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.
If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.
将一个或多个数组的元素合并在一起,以便将 1 的值附加到前一个数组的末尾。它返回结果数组。
如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个。但是,如果数组包含数字键,则后面的值不会覆盖原始值,而是会附加。
如果只给出一个数组并且该数组是数字索引的,则键会以连续的方式重新索引。
回答by Tarka
I think this is just undocumented behaviour, but I'm probably wrong about that. Either way, if you're trying to put arrays together like that, use array_merge
我认为这只是无证行为,但我可能错了。无论哪种方式,如果你想像这样将数组放在一起,请使用array_merge
回答by Matteo Riva
When working on arrays, the plus operator doesn't overwrite indexes, nor does it reindex the arrays. In your example chas index 0 just as a, so it's discarded. Use array_merge.
在处理数组时,加号运算符不会覆盖索引,也不会重新索引数组。在您的示例c中,索引 0 与 一样a,因此被丢弃。使用 array_merge。
回答by el Dude
array_splice($a,count($a),0,$b); //array $a becomes a group of $a and $b arrays.
P.S. it's for indexed arrays (not associative)
PS它用于索引数组(非关联)

