The krsort Function
The krsort function sorts an array
by keys in descending order.
The function modifies the array itself.
Syntax
krsort(array &$array, int $flags = SORT_REGULAR): bool
Example
Let's sort an array by keys in descending order:
<?php
$arr = [
'b' => 1,
'e' => 3,
'c' => 2,
'a' => 5,
'd' => 4,
];
krsort($arr);
var_dump($arr);
?>
Code execution result:
[
'd' => 4,
'e' => 3,
'c' => 2,
'b' => 1,
'a' => 5,
]
See Also
-
the
sortfunction,
which sorts by elements in ascending order -
the
rsortfunction,
which sorts by elements in descending order -
the
ksortfunction,
which sorts by keys in ascending order -
the
asortfunction,
which sorts by elements in ascending order while preserving keys -
the
arsortfunction,
which sorts by elements in descending order while preserving keys -
the
natsortfunction,
which sorts using a natural order algorithm -
the
natcasesortfunction,
which sorts using a case-insensitive natural order algorithm -
the
usortfunction,
which sorts using a user-defined comparison function -
the
uksortfunction,
which sorts by keys using a user-defined comparison function -
the
uasortfunction,
which sorts using a user-defined comparison function and maintains index association -
the
array_multisortfunction,
which sorts multiple arrays