krsort()

krsort() — Sort an array by key in descending order.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

Signature

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

Parameters

ParameterTypeWhat it takes
&$arrayarrayThe input array.
$flagsintSorting type flags: SORT_REGULAR - compare items normally; the details are described in the comparison operators section SORT_NUMERIC - compare items numerically SORT_STRING - compare items as strings SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale() SORT_NATURAL - compare items as strings using "natural ordering" like natsort() ...

Return value

Always returns true.

Example

A runnable snippet:

$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); krsort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val\n"; }

Output:

d = lemon c = apple b = banana a = orange

Changelog

VersionChange
8.2.0The return type is true now; previously, it was bool.
8.2.0This function now does numeric string comparison under SORT_REGULAR using the standard PHP 8 rules.

See also

sort(), ksort()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page krsort() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.