I'm struggling merging subsets of an array in a loop.
I have a big array that can contain any amount of $data records (depending on the API results)
When I try to merge just 2 'products' subsets this works just fine:
$merged=array_merge($data[0][products], $data[1][products]);
But since I don't know in advance how many $data records I will end up with I decided to do it in a loop, something like this:
foreach ($data as $products) {
$merged=array_merge($merged,$products[products]);
}
This doesn't work, I've been playing around with this for a couple of hours, but haven't been able to get it to work.
This is an ideal situation to use call_user_func_array
in combination with array_column
:
$merged = call_user_func_array('array_merge', array_column($data, 'products'));