PHP krsort function tutorial shows how to sort arrays by key in reverse order in PHP. Learn krsort with practical examples.
last modified March 13, 2025
The PHP krsort function sorts an array by key in reverse order. It maintains index association, making it ideal for associative arrays.
The krsort function sorts an array by keys in descending order. It returns true on success and false on failure. The sorting is done in-place.
Syntax: krsort(array &$array, int $flags = SORT_REGULAR): bool. The optional $flags parameter modifies the sorting behavior.
This demonstrates sorting an associative array by keys in reverse order.
basic_krsort.php
<?php
$fruits = [ “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. The array is sorted by keys in reverse alphabetical order while maintaining key-value pairs.
krsort can sort arrays with numeric keys in descending order.
numeric_keys.php
<?php
$numbers = [ 10 => “ten”, 2 => “two”, 5 => “five”, 8 => “eight” ];
krsort($numbers);
print_r($numbers);
Output shows keys sorted in descending numeric order: 10, 8, 5, 2. The function correctly handles numeric comparisons when sorting the keys.
The $flags parameter changes how keys are compared during sorting.
sorting_flags.php
<?php
$mixed = [ “10” => “ten”, “2” => “two”, “5” => “five”, “8” => “eight” ];
krsort($mixed, SORT_NUMERIC);
print_r($mixed);
With SORT_NUMERIC, string keys are treated as numbers. Output shows proper numeric ordering: 10, 8, 5, 2 despite being string keys.
krsort can sort arrays with complex keys like arrays or objects.
multi_dimensional.php
<?php
$items = [ [“id” => 3, “name” => “C”], [“id” => 1, “name” => “A”], [“id” => 4, “name” => “D”], [“id” => 2, “name” => “B”] ];
// Create keys from id values $keyed = array_column($items, null, “id”);
krsort($keyed);
print_r($keyed);
This first creates an array with IDs as keys, then sorts them in reverse order. The output shows items sorted by ID in descending order (4, 3, 2, 1).
Combine krsort with array_change_key_case for case-insensitive sorting.
case_insensitive.php
<?php
$colors = [ “Red” => “#FF0000”, “GREEN” => “#00FF00”, “blue” => “#0000FF”, “Yellow” => “#FFFF00” ];
// Convert keys to lowercase for consistent sorting $lowerKeys = array_change_key_case($colors, CASE_LOWER); krsort($lowerKeys);
print_r($lowerKeys);
This converts all keys to lowercase before sorting. Output shows keys sorted in reverse alphabetical order: yellow, red, green, blue (case-insensitive).
Associative Arrays: Use krsort primarily with associative arrays.
Performance: Be mindful with large arrays as it sorts in-place.
Flags: Choose appropriate sorting flags for your data type.
Stability: Remember that krsort is not a stable sort.
This tutorial covered the PHP krsort function with practical examples showing its usage for reverse key sorting scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.