Example 1 — Rank Scores High to Low
Sort a score table so the best player comes first.
<?php
$scores = ["Ann" => 80, "Ben" => 95, "Cara" => 70];
arsort($scores);
foreach ($scores as $name => $score) {
echo $name, ": ", $score, PHP_EOL;
} How It Works
- The array links each name to a score.
- arsort() reorders the items by score, largest first.
- Each name stays connected to its own score.
- The loop prints the leaderboard from top to bottom.

