PHP:添加阵列一起 [英] PHP: Adding arrays together
问题描述
有人能帮助我解释一下吗?我有code两个片段,一期工程如我所料,但对方却没有。
Could someone help me explain this? I have two snippets of code, one works as I expect, but the other does not.
这工作
$a = array('a' => 1, 'b' => 2);
$b = array('c' => 3);
$c = $a + $b;
print_r($c);
// Output
Array
(
[a] => 1
[b] => 2
[c] => 3
)
这不
$a = array('a', 'b');
$b = array('c');
$c = $a + $b;
print_r($c);
// Output
Array
(
[0] => a
[1] => b
)
这是怎么回事?上为什么没有第二个版本还添加了两个数组在一起吗?我有什么误解?我应该怎么做呢?或者是在PHP中的错误?
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?
推荐答案
这是记载和正确的:<一href=\"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.
所以,我想这不是在PHP中的错误,什么是假设发生。我没有注意到在此之前无论是。
So I guess it's not a bug in php and what is suppose happen. I hadn't noticed this before either.
这篇关于PHP:添加阵列一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!