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 returnstrueon 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) orrsort()(reindex).
Syntax of krsort()
The syntax of the krsort() function is as follows:
krsort(array &$array, int $flags = SORT_REGULAR): trueHere, $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 withSORT_STRINGorSORT_NATURALto 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
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
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
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.
Related sorting functions
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?
| Function | Sorts by | Order | Keeps key association |
|---|---|---|---|
ksort() | Key | Ascending | Yes |
krsort() | Key | Descending | Yes |
asort() | Value | Ascending | Yes |
arsort() | Value | Descending | Yes |
sort() | Value | Ascending | No (reindexed) |
rsort() | Value | Descending | No (reindexed) |
uksort() | Key | Custom callback | Yes |
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.