PHP krsort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, krsort() sorts an array by its keys in reverse (descending) order and keeps the key and value pairs together.

The "kr" stands for key reverse. The array is changed in place, so you do not use the return value. The values stay attached to their original keys, only the order of the pairs changes.

Syntax

Call krsort() with the values you need. Start with this line.

PHP
krsort($scores);
CallWhat it does
krsort($scores)Sorts the pairs from the highest key to the lowest key.
krsort($scores, SORT_STRING)Compares the keys as text instead of as numbers.
ksort($scores)The ascending version of the same sort.

krsort() changes the original array and returns true. Keys stay linked to their values. To sort by value instead, use arsort() or rsort().

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

Parameters

ParameterDescription
arrayRequired. The array to sort. It is passed by reference and changed in place.
flagsOptional. How to compare the keys. SORT_REGULAR (default), SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL, and SORT_FLAG_CASE combined with SORT_STRING or SORT_NATURAL.

Return Value

Return ValueDescription
trueAlways returns true. The sorted result is stored in the array you passed in.

PHP Examples

Example 1 — Sort Keys from Z to A

Each pair stays together while the keys are put in descending order.

PHP
<?php
$scores = ["a" => 1, "c" => 3, "b" => 2];

krsort($scores);

foreach ($scores as $key => $value) {
    echo $key, " => ", $value, PHP_EOL;
}

How It Works

  1. $scores has the keys a, c and b in mixed order.
  2. krsort() sorts the pairs by key, from largest to smallest.
  3. Each value moves together with its key.
  4. The foreach loop prints the pairs in the new order: c, b, a.

Example 2 — Numeric Keys Are Sorted as Numbers

With the default flag, number keys are compared by their numeric value, so 33 comes before 10 and 2.

PHP
<?php
$items = [10 => "ten", 2 => "two", 33 => "thirty-three"];

krsort($items);

foreach ($items as $key => $value) {
    echo $key, " => ", $value, PHP_EOL;
}

How It Works

  1. The keys are the integers 10, 2 and 33.
  2. krsort() compares them as numbers in descending order.
  3. That puts 33 first, then 10, then 2.
  4. The values follow their keys, which the loop output shows.

Example 3 — Change the Comparison with SORT_STRING

The flags change how keys are compared. Comparing as text gives a different order than comparing as numbers.

PHP
<?php
$data = [10 => "a", 9 => "b", 2 => "c"];

krsort($data);
echo implode(",", array_keys($data)), PHP_EOL;

krsort($data, SORT_STRING);
echo implode(",", array_keys($data)), PHP_EOL;

How It Works

  1. The default flag compares the keys as numbers, so the descending order is 10, 9, 2.
  2. array_keys() collects the keys and implode() joins them into one line.
  3. SORT_STRING compares the keys as text, character by character.
  4. As text, "9" is greater than "2", and "2" is greater than "10", so the order becomes 9, 2, 10.

Frequently Asked Questions

It sorts an array by its keys in descending order and keeps each key attached to its value.
krsort() sorts by key and keeps the keys. rsort() sorts by value, throws the old keys away and gives the result new keys 0, 1, 2.
No. It changes the array you pass in and returns true.
Use ksort(). It does the same thing in the opposite direction.
Pass SORT_NATURAL as the flag. It treats the numbers in the text in a natural way, so "item10" comes after "item2" in ascending order.

Next: ksort()

ksort() sorts an array by its keys in ascending order and keeps the key and value pairs to.

ksort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful