PHP arsort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, arsort() sorts an array by its values from highest to lowest and keeps the keys attached to their values.

The "a" means associative (keys are kept) and the "r" means reverse. It is ideal for leaderboards, such as listing names by score.

Syntax

Call arsort() with the values you need. Start with this line.

PHP
arsort($scores);
CallWhat it does
arsort($scores)Sorts by value from high to low, keeping keys.
arsort($items, SORT_STRING)Sorts values as strings in reverse order.
$ok = arsort($data)Always returns true.

arsort() sorts the array itself instead of returning a new one. Use asort() for the opposite order, and rsort() if you do not need to keep the keys.

Library declarations
PHP
arsort(array &$array, int $flags = SORT_REGULAR): true

Parameters

ParameterDescription
arrayRequired. The array to sort. It is passed by reference.
flagsOptional. Changes how values are compared: SORT_REGULAR (default), SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL and SORT_FLAG_CASE.

Return Value

Return ValueDescription
trueAlways returns true. The array is sorted in place.

PHP Examples

Example 1 — Rank Scores High to Low

Sort a score table so the best player comes first.

PHP
<?php
$scores = ["Ann" => 80, "Ben" => 95, "Cara" => 70];
arsort($scores);

foreach ($scores as $name => $score) {
    echo $name, ": ", $score, PHP_EOL;
}

How It Works

  1. The array links each name to a score.
  2. arsort() reorders the items by score, largest first.
  3. Each name stays connected to its own score.
  4. The loop prints the leaderboard from top to bottom.

Example 2 — arsort() vs rsort()

arsort() keeps keys, while rsort() renumbers them.

PHP
<?php
$a = [3, 1, 2];
arsort($a);
echo json_encode($a), PHP_EOL;

$b = [3, 1, 2];
rsort($b);
echo json_encode($b), PHP_EOL;

How It Works

  1. Both arrays start as 3, 1, 2 with keys 0, 1, 2.
  2. arsort() sorts the values but keeps their original keys (0, 2, 1).
  3. Because the keys are out of order, json_encode() shows an object.
  4. rsort() gives new keys 0, 1, 2, so the result is a plain list.

Example 3 — Sort Flags: Numbers vs Strings

The flag changes how values are compared.

PHP
<?php
$items = ["10", "9", "2", "1"];

$regular = $items;
arsort($regular);
echo implode(", ", $regular), PHP_EOL;

$text = $items;
arsort($text, SORT_STRING);
echo implode(", ", $text), PHP_EOL;

How It Works

  1. The values look like numbers but are stored as strings.
  2. With the default flag PHP compares them as numbers, so 10 is the largest.
  3. With SORT_STRING they are compared as text, so "9" comes before "10".
  4. Pick the flag that matches how your data should be ordered.

Frequently Asked Questions

It sorts an array by value in descending order and keeps the key-value pairs together.
arsort() keeps the keys. rsort() discards them and renumbers from 0.
arsort() sorts by value. krsort() sorts by key. Both sort in descending order.
No. It sorts the array in place and returns true.
Use asort().

Next: asort()

asort() sorts an array by its values from lowest to highest and keeps the keys attached to.

asort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful