繁体   English   中英

一个更好的php array_merge

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM