2 回答
TA贡献1804条经验 获得超2个赞
如果已经设置了成绩,则不应更新您的记录。如果您打印查询而不是执行它,您将看到您设置了第 1 级,然后您用第 2 级覆盖它,然后用第 3 级覆盖它。
TA贡献1859条经验 获得超6个赞
我认为你想要的代码是这样的:
<?php
$scores_AND_ID = 'M7492F=97.75,M7973O=96,M3487I=94,M2377O=100,M2727B=100,M5821K=100,M7969O=93.13,M1452V=92.5,M4653O=92.38,M4158J=92.25,M2881A=89.38,M6112S=28.63,';
// Gets all of the entries from the source string
$scores_array = array_unique(array_filter(explode(',', $scores_AND_ID)));
// Sets up an associative array of values (student id => score)
$score_map = [];
foreach ($scores_array as $record) {
[ $student_id, $score ] = explode('=', $record);
$score_map[$student_id] = $score;
}
// Ensure values are sorted numerically descending
uasort($score_map, function ($a, $b) {
if ($a > $b) {
return -1;
}
if ($a == $b) {
return 0;
}
return 1;
});
// Gets the maximum value from the scores
$previous_score = max(array_values($score_map));
$rank = 1;
$incrementer = 0;
foreach ($score_map as $student => $score) {
// If the score hasn't changed from it's previous value
// we increment a counter instead of the rank
if ($score == $previous_score) {
$incrementer++;
// Once it's changed, we update the rank based on the incrementer
// and then reset the incrementer
} else {
$rank += $incrementer;
$incrementer = 1;
}
$previous_score = $score;
// Updating database is left as an exercise to the reader
}
- 2 回答
- 0 关注
- 106 浏览
添加回答
举报