如果存在平局,如何对具有重复值的数组值进行排名并跳过某些位置? [英] How do I rank array values with duplicate values and skipping some positions if there is a tie?
问题描述
我正在处理一个操纵大学生考试成绩的数据库.基本上,我是从MySq数据库中提取记录,在任何给定时间添加一个类.我想对学生的排名进行排名,其中perfomer的排名最高,为第一名.
这是一个例子;
I am working on a Database that manipulates college students exam results. Basically, I am pulling the records from a MySq Database, puling one class at any given time. I want to rank rank the students, with the highest perfomer at number 1.
Here is an illustration;
Marks: 37, 92, 84, 83, 84, 65, 41, 38, 38, 84.
所以我想将mysql数据捕获为单个数组.将数据放入数组后,我应该为每个学生分配班级中的职位,例如1/10(数字1,92分),4/10等.现在的问题是,如果有平局,那么下一个分数将跳过一个位置,如果一个位置有3个分数,则下一个分数将跳过2个位置.因此,以上得分将排名如下;
So I want to capture mysql data as a single array. Once I have the data in an array, I should then assign each student a position in the class such as 1/10 (number 1, the 92 score), 4/10 etc. Now the problem is that if there is a tie, then the next score skips a position and if there are 3 scores at one position then the next score skips 2 positions. So the scores above would be ranked as follows;
92 - 1
84 - 2,
84 - 2,
84 - 2,
83 - 5,
65 - 6,
41 - 7,
38 - 8,
38 - 8 ,
37 - 10
评分系统要求保持职位数(如果需要的话,排名),因此我们在该班级中最终获得10个职位,因为职位3、4、5和9没有任何人. (填写每个数字的替代方法只会给我们8个职位!)
The grading system requires that the number of positions (ranks, if you will) will be maintained, so we ended up with 10 positions in this class since positions 3, 4, 5 and 9 did not have any occupants. (The alternative of filling every number will have given us only 8 positions!)
是否有可能(人为可能,/php可能)使用PHP来对上述分数进行排名,以使其能够处理可能的联系,例如在一个位置上获得4个分数?抱歉,我无法提供执行此操作的功能.我需要一个PHP函数(或其他一些PHP),该函数需要一个数组并产生如上所述的排名.
Is it possible (humanly possible /php possible) to use PHP to rank the scores above in such a way that it can handle possible ties such as 4 scores at one position? SADLY, I could not come up with a function to do this. I need a PHP function (or something ... PHP) that will take an array and produce a ranking as above.
尽管我认为我可能会要求太多,但任何帮助都会深表感谢.如果可以在没有数组的情况下使用MySQL查询数据执行此操作,那么这也将有所帮助!
Any help will be deeply appreciated, though I think I may be asking for too much. If it's possible to do this with MySQL query data without having it in an array, then that will also be helpful!
推荐答案
我假设成绩已经由数据库排序,否则请使用sort($grades);
.
I assume the grades are already sorted by the database, otherwise use sort($grades);
.
代码:
$grades = array(92, 84, 84, 84, 83, 65, 41, 38, 38, 37);
$occurrences = array_count_values($grades);
$grades = array_unique($grades);
foreach($grades as $grade) {
echo str_repeat($grade .' - '.($i+1).'<br>',$occurrences[$grade]);
$i += $occurrences[$grade];
}
结果:
92 - 1
84 - 2
84 - 2
84 - 2
83 - 5
65 - 6
41 - 7
38 - 8
38 - 8
37 - 10
编辑 (回复下面的讨论)
很明显,如果平局发生在最低分,
所有最低分数的等级应等于总分数.
Apparently, in case the tie occurs at the lowest score,
the rank of all lowest scores should be equal to the total count of scores.
代码:
$grades = array(92, 84, 84, 84, 83, 65, 41, 38, 37, 37);
$occurrences = array_count_values($grades);
$grades = array_unique($grades);
foreach($grades as $grade) {
if($grade == end($grades))$i += $occurrences[$grade]-1;
echo str_repeat($grade .' - '.($i+1).'<br>',$occurrences[$grade]);
$i += $occurrences[$grade];
}
结果:
92 - 1
84 - 2
84 - 2
84 - 2
83 - 5
65 - 6
41 - 7
38 - 8
37 - 10
37 - 10
这篇关于如果存在平局,如何对具有重复值的数组值进行排名并跳过某些位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!