Home > Article > Backend Development > How to implement the rank function function in excel in php, excelrank_PHP tutorial
The example in this article describes how PHP implements the rank function in excel. Share it with everyone for your reference. The specific analysis is as follows:
SQL statement implementation ranking is like this:
The total scores are 195, 180, 180, and 161, and the rankings are 1, 2, 3, and 4 respectively. In case of a tie, the order is also followed,
The results obtained by the Excel function rank ranking are 1, 2, 2, 4. When encountering a tie, the middle 3 is skipped
The following function simulates this situation
The function is as follows (I don’t know if there is a better implementation method):
The formula is: Rank = total number of people - number of numbers smaller than yourself - number of repetitions of this score + 1 (plus yourself)
The obtained array of rankings is then written to the database according to the corresponding id, thereby realizing the ranking calculation function
(Of course, this can also be changed to 195, 180, 180, 165, and the ranking is 1, 2, 2, 3)
//Get a number smaller than your own
function gt_array_values($val,array $array){
$num=0;
for($i=0;$i
$num++;
}
}
return $num;
}
//Get the number of repetitions of this number
function get_array_repeats($string,array $array) {
$count = array_count_values($array);
foreach ($count as $key => $value) {
if ($key == $string) {
return $value;
}
}
}
I hope this article will be helpful to everyone’s PHP programming design.