The method of PHP to implement rank function in Excel _php skill

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.