在我试图找到带有关系的完美排名解决方案时,我一直坚持给出正确的排名。代码将解释我想要更好地实现的目标:
我有以下数组:
$standings = array(
'player_1' => 30,
'player_2' => 26,
'player_3' => 28,
'player_9' => 28
);
Run Code Online (Sandbox Code Playgroud)
现在为了用关系对它进行排名,我有以下功能:
function setRanking($standings) {
$rankings = array();
arsort($standings);
$rank = 1;
$tie_rank = 0;
$prev_score = -1;
foreach ($standings as $name => $score) {
if ($score != $prev_score) { //this score is not a tie
$count = 0;
$prev_score = $score;
$rankings[$name] = array('score' => $score, 'rank' => $rank);
} else { //this score is a tie
$prev_score = $score;
if ($count++ …Run Code Online (Sandbox Code Playgroud)