Content · Programming · PHP

PHP krsort() Function: Usage and Examples

Learn krsort() with clear PHP examples, output, practical scenarios, important behaviors, and common beginner mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP developers sometimes need to sort an array by its keys in descending order. This comes up when preparing data, displaying results, or integrating information with other services.

The krsort() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.

What Is krsort() in PHP?

Sort an array by its keys in descending order.

Its syntax is:

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

The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.

Important: It modifies the original array, retains key associations, and the flags select key-comparison rules.

Parameters:

  • &$array — May be updated by the function.
  • $flags — A named input or configuration argument; use the declared type and optional default shown above.

Example 1: Basic Usage

This first example uses a small input to demonstrate the main result of krsort().

<?php

$stock = ["A" => 2, "C" => 9, "B" => 4]; krsort($stock); print_r($stock);

Output:

Array
(
    [C] => 9
    [B] => 4
    [A] => 2
)

The output demonstrates the primary operation: sort an array by its keys in descending order.

Example 2: Understanding Another Case

This second example changes the input or option to reveal a different behavior of krsort().

<?php

$scores = [2 => "two", 10 => "ten", 1 => "one"]; krsort($scores, SORT_NUMERIC); print_r($scores);

Output:

Array
(
    [10] => ten
    [2] => two
    [1] => one
)

Compared with the first example, this one illustrates the same operation with a different input shape or option. It modifies the original array, retains key associations, and the flags select key-comparison rules.

Practical Example: A Real Application Scenario

Here is a practical use of krsort() with realistic sample information. Adapt the values to match your application.

<?php

$versions = [2023 => "old", 2025 => "new", 2024 => "mid"]; krsort($versions, SORT_NUMERIC); foreach ($versions as $year => $value) { echo "$year: $value\n"; }

Output:

2025: new
2024: mid
2023: old

This is a useful starting point, but production code should check errors and validate values before performing the operation. It modifies the original array, retains key associations, and the flags select key-comparison rules.

Important Behavior and Edge Cases

It modifies the original array, retains key associations, and the flags select key-comparison rules.

  • Input: Use the expected PHP types and validate user-provided values.
  • Result: Check the return value separately from any modifications to the input.
  • Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.

Comparison With Related Functions

Function or ApproachWhen to Use It
krsort()Sort an array by its keys in descending order.
Related functionsksort() sorts keys ascending; arsort() sorts values descending.

Common Beginner Mistakes

MistakeCorrect Approach
Expecting a sorted copy without changing the inputThese sort functions modify the array directly; copy it first if you need the original.
Using the wrong sort targetDecide whether to sort keys or values, and whether key associations must remain intact.
Assuming every comparison handles text in the same wayksort() sorts keys ascending; arsort() sorts values descending.

Key Takeaways

  • krsort() lets you sort an array by its keys in descending order.
  • It modifies the original array, retains key associations, and the flags select key-comparison rules.
  • ksort() sorts keys ascending; arsort() sorts values descending.
  • Check input, return values, and environmental requirements when adapting the examples.

krsort() provides a focused PHP operation for sort an array by its keys in descending order. Start with the simple examples, then adapt the practical example with the correct checks for your project.

Share this resourceSend this page to someone who may find it useful.