Best php array_merge

I am looking to make it better, without the need for hard coding of integers for $justPrices[$i]:

$pricesResult = array_merge($justPrices[0], $justPrices[1], $justPrices[2], $justPrices[3]);

$justPrices- a multidimensional array containing 4 "bands" of prices within each array. Data for $justPrices, for example:

Array ( [0] => Array ( [0] => 40.95 [1] => 39.95 [2] => 39.45 [3] => 38.95 ) [1] => Array ( [0] => 45.80 [1] => 41.80 [2] => 41.50 [3] => 41.40 ) [2] => Array ( [0] => 45.95 [1] => 42.95 [2] => 41.95 [3] => 41.45 ) [3] => Array ( [0] => 50.00 [1] => 50.00 [2] => 50.00 [3] => 50.00 ) )

The problem is that the number of arrays within $justPriceswill vary from 2 to 10+. Therefore, I need the parameters for the function to array_merge()vary depending on the number of arrays within $justPrices. I was going to use this simple method to get the number of arrays in $justPrices:

$justPricesMax = count($justPrices);

I could write for loop, and I could still, I was just wondering if there is a better way for what seems to be relatively simple on the surface!

+5
source share
1 answer

If you just want to smooth the array, you can use as parameters call_user_func_arrayto call array_mergewith elements $justPrices:

$flat = call_user_func_array('array_merge', $justPrices);

This is equivalent to calling a function:

$flat = array_merge($justPrices[0], $justPrices[1], … , $justPrices[count($justPrices)-1]);
+10
source

All Articles