krsort
Sorts an array in place by key in descending order, keeping every key attached to its value.
krsort(array &$array, int $flags = SORT_REGULAR): true- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The array to sort. Passed by reference and modified in place. |
$flagsoptional | int= SORT_REGULAR | Comparison mode: SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING or SORT_NATURAL, optionally OR-ed with SORT_FLAG_CASE. |
Return value
trueAlways returns true. Since PHP 8.2.0 the declared return type is true; before that it was bool.
Changelog
- PHP 8.2.0The return type is true now; previously it was bool.
- PHP 8.2.0Numeric strings are now compared numerically under SORT_REGULAR, following the standard PHP 8 comparison rules.
- PHP 8.0.0Sorting is stable: keys that compare as equal keep their original relative order.
See also
krsort reorders an array by its keys, largest first, without touching the key-to-value pairing. It is the descending counterpart of ksort.
When to reach for it
Any array keyed by something naturally ordered where you want the highest first: a date => totals map shown newest-first, a version => changelog array, or per-year statistics on a report. Because array order in PHP is insertion order rather than key order, an array built by appending days as they arrive is not sorted at all until you sort it.
What trips people up
Like all PHP sorts it works in place and returns true, so $sorted = krsort($data) destroys the array.
Key types matter more than most people expect. PHP silently converts numeric-string keys such as '10' to the integer 10, but leaves 'v10' or '2026-01' as strings. Mixed integer and string keys under SORT_REGULAR follow PHP 8's comparison rules, which changed in 8.2 for numeric strings — pass SORT_STRING or SORT_NATURAL explicitly when the keys are textual.
SORT_NATURAL is what you want for keys like item2 and item10; plain string comparison puts item10 before item2.
ISO date strings (2026-01-05) sort correctly as plain strings, which is a good reason to key by ISO dates rather than by any other format.
Example
When to use it
- Show a date-keyed metrics array newest day first.
- List a version => release-notes map from latest release down.
- Order a year-keyed revenue summary for a report that reads most-recent-first.
Frequently asked questions
What is the difference between krsort and arsort?
krsort sorts by key in descending order, while arsort sorts by value in descending order. Both preserve the key-value association.
Why are my numeric string keys sorting oddly?
PHP converts numeric-string keys like '10' into integers when they are stored, while keys like 'v10' stay strings, so the array can end up with mixed key types. Pass SORT_STRING or SORT_NATURAL to force a consistent comparison.
Does krsort return the sorted array?
No, it sorts through a reference parameter and always returns true. Call it as a statement, then use the original variable.
More examples
Natural order for versioned keys
SORT_STRING compares character by character, so '2' beats '1' at position 1 and v2 lands first. SORT_NATURAL reads the digit runs as numbers and gives the intuitive descending order v10, v2, v1.
<?php
$notes = ['v1' => 'first', 'v10' => 'tenth', 'v2' => 'second'];
$plain = $notes;
krsort($plain, SORT_STRING);
krsort($notes, SORT_NATURAL);
echo implode(',', array_keys($plain)), "\n"; // v2,v10,v1
echo implode(',', array_keys($notes)), "\n"; // v10,v2,v1ISO date keys sort as plain strings
Zero-padded ISO 8601 dates compare correctly with a plain string sort, so the output runs 2026-01-10, 2026-01-09, 2025-12-31 with no date parsing required. Keys in formats like 9/1/2026 would not behave this way.
<?php
$hits = ['2026-01-09' => 12, '2026-01-10' => 30, '2025-12-31' => 4];
krsort($hits, SORT_STRING);
print_r($hits);krsort versus arsort
krsort orders by the keys themselves, giving c, b, a alphabetically descending. arsort orders by the values 3, 2, 1, which puts b first even though it is not the highest key.
<?php
$data = ['b' => 3, 'a' => 1, 'c' => 2];
$byKey = $data; krsort($byKey);
$byValue = $data; arsort($byValue);
echo implode(',', array_keys($byKey)), "\n"; // c,b,a
echo implode(',', array_keys($byValue)), "\n"; // b,c,a
Discussion