PHP 8 reference · Array Sorting Functions

PHP krsort() Function

Sort an array by key in descending order, keeping each value with its key.

PHP 8 Since 4.0

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

Syntax

krsort(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

Newest date firstTry it yourself
<?php
$events = ["2024-05-01" => "launch", "2026-01-10" => "v2", "2025-03-15" => "beta"];
krsort($events);
print_r($events);

Output

Array
(
    [2026-01-10] => v2
    [2025-03-15] => beta
    [2024-05-01] => launch
)
Numeric keysTry it yourself
<?php
$sizes = [10 => "M", 2 => "XS", 14 => "L"];
krsort($sizes);
echo implode(", ", $sizes), "\n";

Output

L, M, XS

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?