本文介绍了一个更好的PHP的array_merge的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做到这更好的方法,而不需要硬code整数为 $ 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);

我可以写一个循环,我可能还在,我只是想知道是否有用于表面比较简单!

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!

推荐答案

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

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

这相当于一个函数调用

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

这篇关于一个更好的PHP的array_merge的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 10:53