Jim R Posted November 17, 2023 Share Posted November 17, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317457-ranking-a-list-based-on-a-count/ Share on other sites More sharing options...
Barand Posted November 17, 2023 Share Posted November 17, 2023 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/317457-ranking-a-list-based-on-a-count/#findComment-1612954 Share on other sites More sharing options...
Jim R Posted November 18, 2023 Author Share Posted November 18, 2023 I apologize. I omitted that I'm pulling these from a database, and that's the mental issue I was having with the array. id | ranking | team The += is clearly not anything I knew existed. Quote Link to comment https://forums.phpfreaks.com/topic/317457-ranking-a-list-based-on-a-count/#findComment-1612962 Share on other sites More sharing options...
Solution Olumide Posted November 18, 2023 Solution Share Posted November 18, 2023 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/317457-ranking-a-list-based-on-a-count/#findComment-1612964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.