43

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?

Svish
  • 138,188
  • 158
  • 423
  • 589
  • 2
    **possible duplicate of [+ operator for array in PHP?](http://stackoverflow.com/questions/2140090/operator-for-array-in-php)** – Gordon May 11 '10 at 15:07
  • Your example is wrong. The second output should be `1 => 'a', 2 => 'b'`. – Tgr May 11 '10 at 15:09
  • @Gordon: No, I sort of did know what the + operator meant, just... misunderstood it a bit :p – Svish May 11 '10 at 15:12
  • @Tgr: It was wrong, but I fixed it now. And that is what I get outputted here now at least... – Svish May 11 '10 at 15:12
  • that's why I linked the other post. So you understand it better ;) – Gordon May 11 '10 at 15:14

6 Answers6

32

This is documented and correct: 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.

jdcantrell
  • 2,561
  • 1
  • 17
  • 12
  • You could say it is a design flaw. `array_merge`-ing two associative arrays leads to strange results when some of the keys are numeric (which is pretty common for arrays representing database records). – Tgr May 11 '10 at 15:13
  • 1
    Actually, it's by design and really useful if you need to merge two arrays and preserve numerical keys (or rather merge arrays where the numerical keys are significant). As others here have said, you should use array_merge(). – Angry Dan Apr 16 '13 at 06:33
26

to be short, this works because if you print_r both $a and $b you have:

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:

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:

$c = $b + $a; // instead of $c = $a + $b;

the result will be:

Array
(
    [0] => c
    [1] => b
)
acm
  • 6,040
  • 2
  • 36
  • 44
21

To add two non-associative arrays you need to use the array_merge function:

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.

Andrew Hare
  • 320,708
  • 66
  • 621
  • 623
1

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

See: http://ca2.php.net/manual/en/function.array-merge.php

Tarka
  • 3,846
  • 2
  • 18
  • 33
1

When working on arrays, the plus operator doesn't overwrite indexes, nor does it reindex the arrays. In your example c has index 0 just as a, so it's discarded. Use array_merge.

Matteo Riva
  • 23,656
  • 11
  • 69
  • 103
0
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)

el Dude
  • 4,115
  • 5
  • 22
  • 39