我正在寻找一种更好的方法,而无需对 $justPrices[$i] 的整数进行硬编码:
I am looking to do this a better way without the need to hardcode the integers for $justPrices[$i]:
$pricesResult = array_merge($justPrices[0], $justPrices[1], $justPrices[2], $justPrices[3]);
$justPrices 是一个多维数组,每个数组中包含 4 个价格波段".$justPrices 的数据例如:
$justPrices is a multidimensional array, containing 4 'bands' of prices within each array. The data for $justPrices being 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 ) )
问题在于 $justPrices 中的数组数量至少会从 2 到 10+ 不等.所以我需要一种方法让 array_merge() 函数的参数根据 $justPrices 中的数组数量而变化.我打算使用这个简单的方法来获取 $justPrices 中数组的数量:
The issue is that the amount of arrays within $justPrices will vary from at least 2 to 10+. So I need a way for the parameters for the array_merge() function to vary dependent on the amount of arrays within $justPrices. I was going to use this simple method to get the amount of arrays within $justPrices:
$justPricesMax = count($justPrices);
我可以写一个 for 循环,我可能仍然如此,我只是想知道是否有更好的方法来处理表面上看起来相对简单的事情!
I could write a for loop, and I might still, I just wondered if there was a better method for what seems on the surface relatively simple!
如果你只是想扁平化数组,可以使用 call_user_func_array 以$justPrices 的元素为参数调用array_merge:
If you just want to flatten the array, you can use call_user_func_array to call array_merge with the elements of $justPrices as parameters:
$flat = call_user_func_array('array_merge', $justPrices);
这相当于一个函数调用:
This is equivalent to a the function call:
$flat = array_merge($justPrices[0], $justPrices[1], … , $justPrices[count($justPrices)-1]);
这篇关于一个更好的 php array_merge的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Zend 中的动作视图助手 - 解决方法?Action View Helper in Zend - Work around?(Zend 中的动作视图助手 - 解决方法?)
这是将 URI 与 PHP 中用于 MVC 的类/方法匹配的好方Is this a good way to match URI to class/method in PHP for MVC(这是将 URI 与 PHP 中用于 MVC 的类/方法匹配的好方法吗)
我在哪里保存 Zend Framework 中的部分(视图),以便Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(视图),以
有一个网站的单一入口点.坏的?好的?没问题?Having a single entry point to a website. Bad? Good? Non-issue?(有一个网站的单一入口点.坏的?好的?没问题?)
MVC + 服务层在 Zend 或 PHP 中常见吗?Is MVC + Service Layer common in zend or PHP?(MVC + 服务层在 Zend 或 PHP 中常见吗?)
PHP MVC 方法中的 Hello World 示例Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)