PHP: Adding arrays together

Posted by Svish on Stack Overflow See other posts from Stack Overflow or by Svish
Published on 2010-05-11T15:01:08Z Indexed on 2010/05/11 15:04 UTC
Read the original article Hit count: 141

Filed under:
|

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
(
    [a] => 1
    [b] => 2
)

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays