Arrays7 min readExamples run on PHP 8.5.10

krsort()

Learn how krsort() sorts a PHP array in place by key from highest to lowest, how its flags change comparison, and which mistakes to avoid.

Signature

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

Purpose

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

  • $array is taken by reference. It must be an array, and it is rearranged directly.
  • $flags controls how keys are compared. The default is SORT_REGULAR, which compares keys using PHP's ordinary comparison rules.

The available flags are:

  • SORT_REGULAR compares keys normally.
  • SORT_NUMERIC compares keys as numbers.
  • SORT_STRING compares keys as strings.
  • SORT_LOCALE_STRING compares keys as strings using the current locale.
  • SORT_NATURAL compares keys as strings using natural ordering, like natsort().
  • SORT_FLAG_CASE can be combined with SORT_STRING or SORT_NATURAL using | 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.

Example
<?php
$fruits = ["d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"];
krsort($fruits);
echo json_encode($fruits, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"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".

Example
<?php
$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";
Output
{"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.

Example
<?php
$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";
Output
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 with krsort() keeps its original indexes, now in reverse order of key.
  • Numeric-string keys become integers. PHP converts keys such as "10" to the integer 10 when the array is built, so SORT_STRING still 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_CASE when 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_REGULAR compares numeric strings with the standard PHP 8 rules.

Common mistakes

  1. Using the return value as the sorted array. $sorted = krsort($data); stores true, not the array. Call krsort($data); and then use $data.
  2. Expecting values to be sorted. krsort() looks only at keys. To sort by value while keeping keys, use arsort().
  3. Forgetting that the original is modified. Make a copy if you still need the original order.
  4. Relying on the default for mixed data. Text such as img12 and img2 needs SORT_NATURAL, and numeric ranking needs SORT_NUMERIC.
  5. Expecting a reindexed list. If you want sequential keys after sorting, use a function such as array_values() on the result.
  • 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

Basic descending sort by string keys
<?php
$fruits = ["d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"];
krsort($fruits);
echo json_encode($fruits, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"d":"lemon","c":"apple","b":"banana","a":"orange"}
Default numeric order versus SORT_STRING
<?php
$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";
Output
{"10":"j","9":"i","2":"b"}
{"9":"i","2":"b","10":"j"}
Natural, case-insensitive key order and the return value
<?php
$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";
Output
true
{"img12":3,"IMG2":2,"img1":1}