This article gives an example of how PHP implements the Rank function in Excel. Share to everyone for your reference. The specific analysis is as follows:
The SQL statement implementation ranking is like this:
Total score is 195,180,180,161, the rank is 1,2,3,4, meet the situation is also in order,
The Excel function rank gets the result of 1,2,2,4, encounters a parallel skip between the 3
The following function simulates this situation.
The functions are as follows (I don't know if there are any better implementations):
The formula is: Rank = total number--the number of numbers smaller than yourself--this score repeats +1 times (plus yourself)
The array of the rankings is then written to the database according to the corresponding ID, and the calculation function of rank is realized.
(Of course this can be changed to such 195,180,180,165, the rank is such 1,2,2,3)
Copy Code code as follows:
An array that gets the rank of a set of numbers
function rank (array $array) {
foreach ($array as $val) {
$repeat =get_array_repeats ($val, $array);
$num =gt_array_values ($val, $array);
$rank []=count ($array)-$num-$repeat +1;
}
return $rank;
}
//Get a number smaller than yourself
function gt_array_values ($val, array $array) {
$ num=0;
for ($i =0; $i <count ($array); $i + +) {
if ($val > $array [$i]) {
$num + +;
}
}
return $num;
}
//Get the repeat number 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 help you with your PHP program design.