Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5959991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:45:47+00:00 2026-05-22T18:45:47+00:00

I am working with database data that manipulates college students exam results. Basically, I

  • 0

I am working with database data that manipulates college students exam results. Basically, I am pulling the records from a MySQL database and pulling one class at any given time. I want to rank the students with the highest performer given the rank of 1.
Here is an illustration;

Marks: 37, 92, 84, 83, 84, 65, 41, 38, 38, 84.  

I want to capture MySQL data as a single array. Once I have the data in an array, I should then assign each student a position in the class such as 1/10 (number 1, the 92 score), 4/10 etc. Now the problem is that if there is a tie, then the next score skips a position and if there are 3 scores at one position then the next score skips 2 positions. So the scores above would be ranked as follows;

92 - 1
84 - 2,
84 - 2,
84 - 2,
83 - 5,
65 - 6,
41 - 7,
38 - 8,
38 - 8 ,
37 - 10

The grading system requires that the number of positions (ranks, if you will) will be maintained, so we ended up with 10 positions in this class since positions 3, 4, 5 and 9 did not have any occupants. (The alternative of filling every number will have given us only 8 positions!)

Is it possible (humanly/programmatically possible) to use PHP to rank the scores above in such a way that it can handle possible ties such as 4 scores at one position? Sadly, I could not come up with a function to do this. I need a PHP function (or something in PHP) that will take an array and produce a ranking as above.

If it’s possible to do this with MySQL query data without having it in an array, then that will also be helpful!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:45:47+00:00Added an answer on May 22, 2026 at 6:45 pm

    I assume the grades are already sorted by the database, otherwise use sort($grades);.

    Code:

    $grades = array(92, 84, 84, 84, 83, 65, 41, 38, 38, 37);
    $occurrences = array_count_values($grades);
    $grades = array_unique($grades);
    foreach($grades as $grade) {
        echo str_repeat($grade .' - '.($i+1).'<br>',$occurrences[$grade]);
        $i += $occurrences[$grade];
    }
    

    Result:

    92 - 1
    84 - 2
    84 - 2
    84 - 2
    83 - 5
    65 - 6
    41 - 7
    38 - 8
    38 - 8
    37 - 10
    

    EDIT (Response to discussion below)

    Apparently, in case the tie occurs at the lowest score,
    the rank of all lowest scores should be equal to the total count of scores.

    Code:

    $grades = array(92, 84, 84, 84, 83, 65, 41, 38, 37, 37);
    $occurrences = array_count_values($grades);
    $grades = array_unique($grades);
    foreach($grades as $grade) {
        if($grade == end($grades))$i += $occurrences[$grade]-1;
        echo str_repeat($grade .' - '.($i+1).'<br>',$occurrences[$grade]);
        $i += $occurrences[$grade];
    }
    

    Result:

    92 - 1
    84 - 2
    84 - 2
    84 - 2
    83 - 5
    65 - 6
    41 - 7
    38 - 8
    37 - 10
    37 - 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a MySQL database that has some data imported from Excel .
I made some CMS in PHP which manipulates with data from MySQL. In my
I am working with Windows form application. In that I retrieve the data from
I'm working on a project where I have to take data from one source
I'm working on a project where there will be a MySQL database containing data
I'm working on an app that collects data from smartcards. I want to be
I'm using a background thread to read some data from my database.The data that
I am working with a oracle database and Jython. I can get data pulled
I am working on a database application that runs on various independent servers. Each
I'm working on a database that needs to represent computers and their users. Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.