php8.5
Home/ Manual/ array / functions/ krsort

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

array

The input array.

Return Values

Always

Changelog

VersionDescription
8.2.0This 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

See Also

Source: reference/array/functions/krsort.xml · from the official PHP manual (php/doc-en)