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.
$premiumBrands = array_merge(..)?$premiumBrands = array_merge($premiumBrands, $brands['premium']);?array_mergereturns a new array, it does not alter the existing ones. Read the docs - php.net/manual/en/function.array-merge.php$premiumBrands + $brands['premium']. See updated Q.array_mergereturns a fresh array, but you have to assign it to a variable to keep it.array_mergedoes not alter any array you give it.