1

I'm unsuccessfully trying to merge arrays. An array looks like this:

//var_dump()
array (size=2)
  0 => 
    object(Brand)[177]
      (...)
  1 => 
    object(Brand)[271]
      (...)

This is the code I use:

        $premiumBrands = array();

        foreach ($stores as $store) :
            $brands = getBrands($store->brands);

            echo count($brands['premium']).', ';

            if(count($brands['premium']) > 0) {
                array_merge($premiumBrands,$brands['premium']);
            }
        endforeach;

        echo count(premiumBrands);

The result of the output in the loop is this: 2, 0, 0, 1, 0;
The result of the last output is this: 0;

Using

$premiumBrands = $premiumBrands + $brands['premium'];

will not work, because all arrays starts with an index key of [0] - so it will just overwrite premiumBrands

So how can I merge my arrays?

And yes, I've read the docs. Still can't solve it.

8
  • 4
    $premiumBrands = array_merge(..)? Commented Jan 6, 2015 at 21:19
  • 3
    $premiumBrands = array_merge($premiumBrands, $brands['premium']);? array_merge returns a new array, it does not alter the existing ones. Read the docs - php.net/manual/en/function.array-merge.php Commented Jan 6, 2015 at 21:22
  • Just a typo. It is $premiumBrands. Commented Jan 6, 2015 at 21:23
  • Yes @Bjorn, I've read the docs. Even tried $premiumBrands + $brands['premium']. See updated Q. Commented Jan 6, 2015 at 21:32
  • 1
    @Steven, you can't sum up arrays as you're trying to do in your updated question. Just replace the line with the code I gave you. The point you're missing is that array_merge returns a fresh array, but you have to assign it to a variable to keep it. array_merge does not alter any array you give it. Commented Jan 6, 2015 at 21:37

1 Answer 1

3

You need to assign the new array to a variable. The new array will contain the merged arrays which you can merge over and over in a loop. It all depends on what your keys and values are to get it to work the way you want, but the documentation is clear http://php.net/manual/en/function.array-merge.php

    $premiumBrands = array();

    foreach ($stores as $store) :
        $brands = getBrands($store->brands);

        echo count($brands['premium']).', ';

        if(count($brands['premium']) > 0) {
            $premiumBrands = array_merge($premiumBrands,$brands['premium']); /// *** ///
        }
    endforeach;

    echo count(premiumBrands);
Sign up to request clarification or add additional context in comments.

2 Comments

Of course. At the end of the day my brain is mush and I thought array_merge did that job for me. #shouldnotworkwhentired
PHP is dirty like that. Some functions return the result, others manipulate the parameters, but in most cases, the result is returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.