Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.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排名_Php_Arrays_Rank - Fatal编程技术网

基于平均值的php排名

基于平均值的php排名,php,arrays,rank,Php,Arrays,Rank,我已经试了一段时间了,但还是被困在这里。实际上,我需要从平均值中得到排名。这是我的结果 Average Rank 39 39 rank is 1 32 32 rank is 1 51 51 rank is 1 57 57 rank is 1 我真正需要的是 Average Rank 39 3 32 4 51

我已经试了一段时间了,但还是被困在这里。实际上,我需要从平均值中得到排名。这是我的结果

Average         Rank
   39        39 rank is 1
   32        32 rank is 1
   51        51 rank is 1
   57        57 rank is 1
我真正需要的是

Average           Rank
  39               3 
  32               4 
  51               2 
  57               1
我试过几种方法,但似乎都不管用。顺便说一句,这是我的代码



从您发布的内容来看,问题似乎在于如何对平均值进行降序排序

考虑到这一点,您知道如果要计算移动中的平均值,必须首先遍历整个数组,因此这里有一些代码片段可以帮助您:

// Random values I want to calculate the average
$values = [
    1 => [15, 25, 35],
    2 => [45, 55, 65],
    3 => [75, 85, 95],
    4 => [105, 115, 125]
];

$avgs = array();
foreach ($values as $key => $value) {
    $avgs[$key] = array_sum($value) / count($value); // calculate average for each key on the array
}

print_r($avgs);
// Will print:
// Array
// (
//    [1] => 25
//    [2] => 55
//    [3] => 85
//    [4] => 115
//)

arsort($avgs); // sort the array in descending order
print_r($avgs);

// Will print:
//Array
//(
//    [4] => 115
//    [3] => 85
//    [2] => 55
//    [1] => 25
//)
因此,如果有你的平均数组排序,因此你有你的“排名”

注:键1、2、3、4只是随机值,你可以在那里找到任何东西。


<?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?> 

<?php 
$array = array($avg); // useless
$i=1;
foreach($array as $key=>$value)  // also useless, you never use these variables, and since $array contains only one entry, there is no need to foreach it
{
    $max = max($array);
    echo "\n".$max." rank is ". $i."\n";
    $keys = array_search($max, $array);    
    unset($array[$keys]);
    if(sizeof($array) >0)
    if(!in_array($max,$array))
        $i++;
}

$array
在您发布的代码中只有一个元素。还有其他(相关的)代码吗?我能给你的唯一建议是:首先构建一个数组,然后开始输出一个表。您现在正试图自行确定一个数字的秩,该值始终为1。@Justastudent如何添加另一个元素。。?顺便说一句,我刚刚发布了我的大代码您发布的大部分代码的可能重复与问题无关,但它足以让我写下我的评论,首先构建一个数组,然后输出一个表。我将一个关于确定数组中数字的秩的问题联系起来。将值
$value
添加到数组
$array
$array[]=$value
一样简单。
<?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?> 

<?php 
$max = $avg; // That's actually what you were doing. Cleaning the code makes it obvious, that's all
echo "\n".$max." rank is ". $i."\n";
$keys = array_search($max, $array);    
unset($array[$keys]);
if(sizeof($array) >0)
if(!in_array($max,$array))
    $i++;
}