关于php:如果$A和$B是两个数组,那么$A+$B的结果是什么?

if $a and $b are bothe arrays the what would be the result of $a+$b?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
+ operator for array in PHP?

如果$a$b都是阵列,那么$a + $b的结果是什么?


http://www.php.net/manual/en/language.operators.array.php

1
Union of $a and $b.

The + operator appends elements of
remaining keys from the right handed
array to the left handed, whereas
duplicated keys are NOT overwritten.


1
2
3
4
5
6
7
<?php

$a = array(1, 2, 3);
$b = array(4, 5, 6);
$c = $a + $b;

print_r($c);

结果是:

1
2
3
4
5
6
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

但是:

1
2
3
4
5
6
7
<?php

$a = array('a' => 1, 'b' => 2, 'c' => 3);
$b = array('d' => 4, 'e' => 5, 'f' => 6);
$c = $a + $b;

print_r($c);

结果:

1
2
3
4
5
6
7
8
9
Array
(
    [a] => 1
    [b] => 2
    [c] => 3
    [d] => 4
    [e] => 5
    [f] => 6
)

所以这里的答案似乎取决于数组的键控方式。


现在试试这个实验

1
2
3
4
5
6
7
8
9
10
11
$a = array( 0 => 1,
            1 => 2,
            4 => 3
          );
$b = array( 2 => 4,
            4 => 5,
            6 => 6
          );
$c = $a + $b;

var_dump($c);

我的测试

1
2
3
4
5
6
7
8
9
10
$ar1 = array('1', '2');
$ar2 = array('3', '4');
$test = $ar1 + $ar2;
print_r($test);

Array
(
    [0] => 1
    [1] => 2
)

如果你做了类似于$result = $a + $b;的事情,那么$result将被分配到第一个参数,在本例中是$a