[英]A better php array_merge
我希望這是一個更好的方法,而無需硬編碼$justPrices[$i]
的整數:
$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
的數組數量至少從2到10+不等。 所以我需要一種方法讓array_merge()
函數的參數根據$justPrices
的數組量而變化。 我打算使用這個簡單的方法來獲取$justPrices
的數組量:
$justPricesMax = count($justPrices);
我可以寫一個for loop
,我可能仍然,我只是想知道是否有更好的方法表面上看起來相對簡單!
如果您只想展平數組,可以使用call_user_func_array
以$justPrices
的元素作為參數調用array_merge
:
$flat = call_user_func_array('array_merge', $justPrices);
這相當於函數調用:
$flat = array_merge($justPrices[0], $justPrices[1], … , $justPrices[count($justPrices)-1]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.