2 回答

TA貢獻1850條經(jīng)驗 獲得超11個贊
您可以通過多種方式實現(xiàn)這一目標。如果這是一個賦值,計數(shù)和循環(huán)將幫助您理解所需的邏輯,但您可以使用內(nèi)置函數(shù)以一種相當緊湊的方式完成此操作:
array_map
將給定的函數(shù)應(yīng)用于數(shù)組的每個元素并返回一個新數(shù)組。array_chunk
拆分包含指定元素數(shù)量的數(shù)組。max
返回數(shù)組的最大值。
把它們放在一起:
$array = [1,2,3,4,15,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,22,42,33,34,35,36,37,38,11,10];
$max = array_map('max', array_chunk($array, 10));

TA貢獻1810條經(jīng)驗 獲得超4個贊
有一些邏輯:
$array = [1,2,3,4,15,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,22,42,33,34,35,36,37,38,11,10];
$lengthOfArray = count($array);
$maxValues = [];
$groupBy = 10;
$tempMaxValue = -1; // A small value that can be comparable
//Loop the array
for ($i=0; $i < $lengthOfArray; $i++) {
if($tempMaxValue<$array[$i]){
$tempMaxValue= $array[$i];
}
// If we passed 10 results:
if($i%$groupBy==9){
$maxValues[] = $tempMaxValue; //Hold the Max
$tempMaxValue = -1; //Reset the temp value
}
}
$maxValues將包含所有結(jié)果。
- 2 回答
- 0 關(guān)注
- 210 瀏覽
添加回答
舉報