krsort
PHP function
Edit on GitHub ✎
(PHP 4, PHP 5, PHP 7, PHP 8)
Sort an array by key in descending order
Description
krsort(array $array, int $flags = SORT_REGULAR): true
Sorts array in place by keys in descending order.
Sort-unstable Reset-index
Parameters
arrayThe input array.
Return Values
Always
Changelog
| Version | Description |
|---|---|
| 8.2.0 | This function now does numeric string comparison under SORT_REGULAR using the standard PHP 8 rules. |
Examples
krsort() example
php
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>The above example will output:
output
d = lemon
c = apple
b = banana
a = orange