Home >Backend Development >PHP Tutorial >How to implement the rank function function in excel in php, excelrank_PHP tutorial

How to implement the rank function function in excel in php, excelrank_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:09:241082browse

How to implement the rank function in excel in php, excelrank

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)

Copy code The code is as follows:
//Get an array of rankings for 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 your own
function gt_array_values($val,array $array){
         $num=0;
for($i=0;$i If($val>$array[$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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/945714.htmlTechArticleHow to implement the rank function function in excel in php, excelrank This article describes the method of using php to implement the rank function function in excel. . Share it with everyone for your reference. The specific analysis is as follows: sql language...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn