W3docs

krsort()

Sorting arrays in PHP is a common task, especially when dealing with large amounts of data. The PHP function array_krsort() is an extremely useful function that

PHP Function krsort(): Sort an Array by Key in Reverse Order

Sorting arrays in PHP is a common task, especially when dealing with large amounts of data. The PHP function krsort() is an extremely useful function that allows developers to sort an array by key in reverse order. This function is very powerful and can save a lot of time and effort when sorting arrays. In this article, we will go through the functionality of the krsort() function in detail and demonstrate how to use it in different scenarios.

What is krsort()?

The krsort() function is a PHP built-in function that sorts an array in reverse order based on its keys, while preserving the association between each key and its value. The name stands for key reverse sort. It is the descending counterpart of ksort(), which sorts by key in ascending order.

Because it sorts by key (not by value), krsort() is most useful with associative arrays or arrays with explicit numeric keys — for example, sorting records keyed by ID, year, or priority so the largest key comes first.

Two things to keep in mind:

  • krsort() sorts the array in place and returns true on success (it never returns the sorted array), so you call it on the variable directly.
  • It works on the array's keys. To sort by value instead, use arsort() (keep keys) or rsort() (reindex).

Syntax of krsort()

The syntax of the krsort() function is as follows:

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

Here, $array is the array to sort (passed by reference, so the original array is modified), and $flags is an optional parameter that controls how keys are compared. The function always returns true. The possible values of $flags are:

  • SORT_REGULAR: This is the default value. The function sorts the elements normally, without any change in their data types.
  • SORT_NUMERIC: The function sorts the elements numerically, i.e., treating them as numbers.
  • SORT_STRING: The function sorts the elements as strings.
  • SORT_LOCALE_STRING: The function sorts the elements as strings, based on the current locale setting.
  • SORT_NATURAL: The function sorts the elements in a natural order, i.e., treating them as strings, but taking into account their numeric values.
  • SORT_FLAG_CASE: This flag can be combined with SORT_STRING or SORT_NATURAL to sort the strings in a case-insensitive manner.

Examples of using krsort()

Now, let's look at some examples of how to use the krsort() function.

Example 1: Sorting an array in reverse order based on its keys

php— editable, runs on the server

Output:

Array
(
    [d] => date
    [c] => cherry
    [b] => banana
    [a] => apple
)

In this example, we have an array with four elements. We are sorting the array in reverse order based on its keys using the krsort() function. The output shows that the array has been sorted in descending order based on its keys.

Example 2: Sorting an array in reverse order based on its keys, numerically

php— editable, runs on the server

Output:

Array
(
    [4] => cherry
    [3] => date
    [2] => apple
    [1] => banana
)

Here the keys are numeric strings. Sorting with the SORT_NUMERIC flag compares them as numbers, so the array ends up ordered from the highest key (4) down to the lowest (1). Note that PHP automatically casts integer-like string keys such as "1" to integers, so SORT_REGULAR would give the same result here — the SORT_NUMERIC flag matters when keys could otherwise be compared as strings (where "10" sorts before "9").

Example 3: Sorting an array in reverse order based on its keys, case-insensitively

php— editable, runs on the server

Output:

Array
(
    [d] => date
    [C] => cherry
    [b] => banana
    [A] => apple
)

In this example, we have an array with four elements, where the keys are strings, some of them in uppercase. We are sorting the array in reverse order based on its keys, case-insensitively, using the krsort() function with the SORT_STRING and SORT_FLAG_CASE flags. The output shows that the array has been sorted in descending order based on its keys, ignoring the case of the strings.

PHP offers a full family of array-sorting functions. Choosing the right one comes down to two questions: do you sort by key or by value, and do you want ascending or descending order?

FunctionSorts byOrderKeeps key association
ksort()KeyAscendingYes
krsort()KeyDescendingYes
asort()ValueAscendingYes
arsort()ValueDescendingYes
sort()ValueAscendingNo (reindexed)
rsort()ValueDescendingNo (reindexed)
uksort()KeyCustom callbackYes

If your sorting logic is too specific for the standard flags — for example, sorting keys by string length — reach for uksort(), which lets you supply your own comparison function.

Conclusion

The krsort() function sorts an array by its keys in descending order, in place, while preserving each key-to-value pair. It accepts an optional sort-flag argument so you can compare keys numerically, as strings, naturally, or case-insensitively. Reach for krsort() whenever you have a keyed dataset — IDs, years, priorities — that you want presented from highest key to lowest, and use its ksort() and arsort() relatives when you need a different order or want to sort by value instead.

Practice

Practice
What does the PHP function krsort() do?
What does the PHP function krsort() do?
Was this page helpful?