Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP数组中找到前10个元素之间的最大值,然后从下10个元素中找到最大值,依此类推?_Php - Fatal编程技术网

如何在PHP数组中找到前10个元素之间的最大值,然后从下10个元素中找到最大值,依此类推?

如何在PHP数组中找到前10个元素之间的最大值,然后从下10个元素中找到最大值,依此类推?,php,Php,在PHP数组中,有1000个元素。我想要的是找到前10个元素之间的“最大值”。然后需要从接下来的10个元素中找到最大值,这个过程一直持续到我到达数组的末尾 $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] 预期产出: 15,10,42有一些逻辑: $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,3

在PHP数组中,有1000个元素。我想要的是找到前10个元素之间的“最大值”。然后需要从接下来的10个元素中找到最大值,这个过程一直持续到我到达数组的末尾

$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]
预期产出: 15,10,42有一些逻辑:

$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
  }
}
$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=计数($array);
$maxValues=[];
$groupBy=10;
$tempMaxValue=-1;//可以比较的小值
//循环数组
对于($i=0;$i<$lengthOfArray;$i++){
如果($tempMaxValue带有某些逻辑:

$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
  }
}
$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=计数($array);
$maxValues=[];
$groupBy=10;
$tempMaxValue=-1;//一个可以比较的小值
//循环数组
对于($i=0;$i<$lengthOfArray;$i++){

如果($tempMaxValue,您可以通过几种方式实现。如果这是一种赋值,计数和循环将帮助您理解所需的逻辑,但您可以使用内置函数以一种相当紧凑的方式实现:

  • 将给定函数应用于数组的每个元素并返回一个新数组
  • 拆分包含指定元素数的数组
  • 返回数组的最大值
总而言之:

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

您可以通过多种方式实现这一点。如果这是一个赋值,计数和循环将帮助您理解所需的逻辑,但您可以使用内置函数以一种相当紧凑的方式实现这一点:

  • 将给定函数应用于数组的每个元素并返回一个新数组
  • 拆分包含指定元素数的数组
  • 返回数组的最大值
总而言之:

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

你为什么要用Javascript标记它?不管怎样,在不为你做作业的情况下,你想做的是使用
array\u slice
函数并迭代数组的每个部分-获取一个片段,对其排序,然后获取最后一个值。直到到达数组的末尾。或者,使用
array\u slice
如上所述,但使用函数e> max
函数,如
$max=max($arr)
为什么要用Javascript标记这个?不管怎样,在不为您做任何准备的情况下,您想要做的是使用
array\u slice
函数并迭代数组的每个部分-获取一个切片,对其排序,然后获取最后一个值。直到到达数组的末尾。或者,使用上面提到的
array\u slice
,但使用
max
函数,如
$max=max($arr);