PHP 8 reference · Array Sorting Functions

PHP arsort() Function

Sort an array by value in descending order, keeping each key attached to its value. Great for leaderboards.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
array The array to sort (changed in place).
flags optional SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_NATURAL, optionally | SORT_FLAG_CASE. Default: SORT_REGULAR

Return value

Always true.

Examples

LeaderboardTry it yourself
<?php
$scores = ["ann" => 72, "bob" => 95, "cal" => 88];
arsort($scores);
print_r($scores);

Output

Array
(
    [bob] => 95
    [cal] => 88
    [ann] => 72
)
Most frequent firstTry it yourself
<?php
$votes = array_count_values(["php", "go", "php", "sql", "php", "go"]);
arsort($votes);
echo array_key_first($votes), " wins\n";

Output

php wins

Version history

VersionChange
PHP 4.0Added
PHP 8.0Sorting is stable: elements that compare equal keep their original order.
PHP 8.2The return type is true (it was bool).

Was this page helpful?