Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

krsort()

Order a map by descending key in place, leaving the value associated with each key.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Order a map by descending key in place, leaving the value associated with each key. Use SORT_NUMERIC when the largest numeric key should be first. It returns true and does not renumber sparse integer keys. Since PHP 8.2 the default SORT_REGULAR compares numeric string keys numerically. Descending, "010" precedes "09", the reverse of SORT_STRING.

Signature

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

Example output

[true,[11,5,2],["eleven","five","two"]]
[["010","09"],["09","010"]]

Examples

<?php
$rows = [2=>"two",11=>"eleven",5=>"five"];
$ok = krsort($rows,SORT_NUMERIC);
echo json_encode([$ok,array_keys($rows),array_values($rows)]);
$padded=["09"=>9,"010"=>10]; $lexical=$padded;
krsort($padded); krsort($lexical,SORT_STRING);
echo PHP_EOL,json_encode([array_keys($padded),array_keys($lexical)]);

Result

[true,[11,5,2],["eleven","five","two"]]
[["010","09"],["09","010"]]

Sources