Signature
krsort(array &$array, int $flags = SORT_REGULAR): truePurpose
krsort() reorders an array by its keys, from the highest key to the lowest. Each key keeps its value, so the key/value pairs stay together and only their order changes. It is the descending counterpart of ksort(). Use it when the keys carry the meaning you want to rank by: version numbers, years, priorities, scores stored as keys, or identifiers that should list newest first.
The function works in place. It modifies the variable you pass and does not return a sorted copy.
Signature
Parameters and defaults
$arrayis taken by reference. It must be an array, and it is rearranged directly.$flagscontrols how keys are compared. The default isSORT_REGULAR, which compares keys using PHP's ordinary comparison rules.
The available flags are:
SORT_REGULARcompares keys normally.SORT_NUMERICcompares keys as numbers.SORT_STRINGcompares keys as strings.SORT_LOCALE_STRINGcompares keys as strings using the current locale.SORT_NATURALcompares keys as strings using natural ordering, likenatsort().SORT_FLAG_CASEcan be combined withSORT_STRINGorSORT_NATURALusing|to ignore letter case.
Return value and failure cases
The return type is true, and the function always returns true. In PHP 8.2.0 the declared return type changed from bool to true. There is no failure value to test, so checking the result is unnecessary. Passing something that is not an array throws a TypeError.
Two side effects are worth remembering. Items that compare as equal keep their original relative order. The array's internal pointer is also reset to the first element.
Example 1: basic descending order
With string keys the default flag compares them alphabetically, and the largest key comes first.
$fruits = ["d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"];
krsort($fruits);
echo json_encode($fruits, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"d":"lemon","c":"apple","b":"banana","a":"orange"}
The keys are now ordered d, c, b, a, and every value moved along with its key.
Example 2: flags change the comparison
Integer keys sort numerically by default. With SORT_STRING they are compared as text, so "9" is greater than "2", which is greater than "10".
$items = [2 => "b", 10 => "j", 9 => "i"];
$regular = $items;
krsort($regular);
$asString = $items;
krsort($asString, SORT_STRING);
echo json_encode($regular, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
echo json_encode($asString, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"10":"j","9":"i","2":"b"}
{"9":"i","2":"b","10":"j"}
Copying the array first keeps the original available for the second sort. Arrays are copied by value in PHP, so sorting one copy does not touch the other.
Example 3: natural order, ignoring case
Keys that contain numbers, such as img12, usually need natural ordering. Combining SORT_NATURAL with SORT_FLAG_CASE also ignores letter case.
$files = ["img1" => 1, "IMG2" => 2, "img12" => 3];
$result = krsort($files, SORT_NATURAL | SORT_FLAG_CASE);
echo json_encode($result), "\n";
echo json_encode($files, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";true
{"img12":3,"IMG2":2,"img1":1}
A plain string comparison would place img2 after img12. Natural ordering treats the digits as whole numbers, so 12 ranks above 2 and 1.
Edge cases
- Keys are preserved. Unlike
rsort(),krsort()does not renumber keys. A list sorted withkrsort()keeps its original indexes, now in reverse order of key. - Numeric-string keys become integers. PHP converts keys such as
"10"to the integer10when the array is built, soSORT_STRINGstill compares them as text but the keys remain integers. - Equal keys keep their order. Items that compare as equal retain their original relative order, which can happen with flags like
SORT_STRING | SORT_FLAG_CASEwhen keys differ only by case. - Mixed key types. An array that mixes integer and string keys is compared according to the chosen flag. If the result matters, pick an explicit flag instead of relying on the default.
- Version notes. Since PHP 8.2.0,
SORT_REGULARcompares numeric strings with the standard PHP 8 rules.
Common mistakes
- Using the return value as the sorted array.
$sorted = krsort($data);storestrue, not the array. Callkrsort($data);and then use$data. - Expecting values to be sorted.
krsort()looks only at keys. To sort by value while keeping keys, usearsort(). - Forgetting that the original is modified. Make a copy if you still need the original order.
- Relying on the default for mixed data. Text such as
img12andimg2needsSORT_NATURAL, and numeric ranking needsSORT_NUMERIC. - Expecting a reindexed list. If you want sequential keys after sorting, use a function such as
array_values()on the result.
Related functions
ksort()sorts by key in ascending order.sort()sorts values in ascending order and reindexes.rsort()sorts values in descending order and reindexes.arsort()sorts by value in descending order and keeps keys.uksort()sorts by key with your own comparison function.
Recap
krsort() sorts an array in place by key from highest to lowest and keeps each key attached to its value. It always returns true, so never assign its result. Choose the flag that matches your keys: numeric, string, or natural, with optional case folding. Copy the array first when you need the original order.
Tested examples
$fruits = ["d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"];
krsort($fruits);
echo json_encode($fruits, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
{"d":"lemon","c":"apple","b":"banana","a":"orange"}
$items = [2 => "b", 10 => "j", 9 => "i"];
$regular = $items;
krsort($regular);
$asString = $items;
krsort($asString, SORT_STRING);
echo json_encode($regular, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
echo json_encode($asString, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
{"10":"j","9":"i","2":"b"}
{"9":"i","2":"b","10":"j"}
$files = ["img1" => 1, "IMG2" => 2, "img12" => 3];
$result = krsort($files, SORT_NATURAL | SORT_FLAG_CASE);
echo json_encode($result), "\n";
echo json_encode($files, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
true
{"img12":3,"IMG2":2,"img1":1}