2010-09-03 41 views
5

我期待这个做一个更好的方式,而不需要硬编码的整数为$justPrices[$i]一个更好的php array_merge

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

$justPrices是一个多维数组,包含每个阵列中的4价的“带”。为$justPrices该数据是例如:

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)) 

的问题是,内$justPrices阵列的量将来自至少两个变化至10+。所以我需要一种方法来使array_merge()函数的参数根据$justPrices中的数组数量而变化。我打算用这个简单的方法来内$justPrices得到阵列量:

$justPricesMax = count($justPrices); 

我可以写一个for loop,我可能还是,我只是想知道是否有在表面上似乎什么更好的方法比较简单!

+0

你期望什么结果? – Gumbo 2010-09-03 10:03:26

+0

@Gumbo包含所有价格的单个数组。我以后用这个平均价格的东西。 – freeMagee 2010-09-03 10:26:37

回答

10

如果你只是想压平的数组,你可以使用call_user_func_array调用array_merge$justPrices元素作为参数:

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

这相当于一个函数调用:

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

+1:爱PHP厨房水槽。 – shamittomar 2010-09-03 10:16:19

+1

这就是字面意思。梦幻般的回应。 – freeMagee 2010-09-03 10:29:32