Using krsort() to Sort an Array by Keys in Reverse Order (PHP)
This article explains how the PHP krsort() function sorts an array by its keys in descending order, describes its parameters and return values, and provides a complete example with code and expected output to illustrate its usage.
The PHP function krsort() sorts an array by its keys in reverse order while preserving key‑value associations.
It accepts the array by reference and an optional sort_flags parameter to modify sorting behavior, returning TRUE on success or FALSE on failure.
Example:
<?php
$fruits = array(
"d" => "lemon",
"a" => "orange",
"b" => "banana",
"c" => "apple"
);
krsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val
";
}
?>The output will be:
d = lemon
c = apple
b = banana
a = orangeSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
