Jump to content

Ranking a list based on a count...


Jim R
Go to solution Solved by Olumide,

Recommended Posts

Looking for advice on what to look for.  Likely sorting arrays, but I can't find an example anywhere that matches the basic gist of what I need.  

 

Multiple people will provide a list.  

Person 1:

1.  Team A

2.  Team B

3. Team C

4.  Team D

 

Person 2

1.  Team C

2. Team B

3.  Team D

4.  Team A

 

It goes on from there.

Each of those teams get a value based on where they are on the list.  1 = 1, 2 = 2, etc.

So Team A would have 5, Team 4, Team C 4, Team D 7.  

I want to rank them according to lowest total value to highest.  I assume I'll be using asort, as well as  a foreach loop.  

 

However, I can't wrap my head around how I would set up the array.  

Link to comment
Share on other sites

Something ike ...

$positions = [ 1 => [ 'Team A' => 1,
                      'Team B' => 2,
                      'Team C' => 3,
                      'Team D' => 4
                    ],
               2 => [ 'Team C' => 1,
                      'Team B' => 2,
                      'Team D' => 3,
                      'Team A' => 4
                    ]
             ];
             
$result = [];

foreach ($positions as $person => $plist)  {
    foreach ($plist as $t => $pos)  {
        if (!isset($result[$t]))  {
            $result[$t] = $pos;
        }
        else {
            $result[$t] += $pos;
        }
    }
}
asort($result);
echo '<pre>' . print_r($result, 1) . '</pre>';

outputs...

Array
(
    [Team B] => 4
    [Team C] => 4
    [Team A] => 5
    [Team D] => 7
)

 

Link to comment
Share on other sites

  • Solution

Here's an example of how you can achieve this:

 

// Assuming you have a PDO connection, replace it with your database connection

// Fetch data from the database
$query = $db->prepare("SELECT id, ranking, team FROM your_table");
$query->execute();
$results = $query->fetchAll(PDO::FETCH_ASSOC);

// Initialize an array to store team scores
$teamScores = [];

// Process each person's ranking
foreach ($results as $row) {
    $ranking = explode(',', $row['ranking']);
    $id = $row['id'];

    // Assign scores to teams based on their rankings
    foreach ($ranking as $key => $team) {
        $team = trim($team);
        if (!isset($teamScores[$team])) {
            $teamScores[$team] = 0;
        }
        $teamScores[$team] += $key + 1; // Assign scores based on the ranking
    }
}

// Sort the teams based on their total scores
asort($teamScores);

// Output the sorted teams and their scores
foreach ($teamScores as $team => $score) {
    echo "Team: $team, Total Score: $score<br>";
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.