PHP: Hypertext Preprocessor (original) (raw)
krsort
(PHP 4, PHP 5, PHP 7, PHP 8)
krsort — Sort an array by key in descending order
Description
Note:
If two members compare as equal, they retain their original order. Prior to PHP 8.0.0, their relative order in the sorted array was undefined.
Note:
Resets array's internal pointer to the first element.
Return Values
Always returns [true](reserved.constants.php#constant.true)
.
Changelog
Version | Description |
---|---|
8.2.0 | The return type is true now; previously, it was bool. |
8.2.0 | This function now does numeric string comparison under**SORT_REGULAR** using the standard PHP 8 rules. |
Examples
Example #1 krsort() example
<?php $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); krsort($fruits); foreach ($fruits as <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mi>e</mi><mi>y</mi><mo>=</mo><mo>></mo></mrow><annotation encoding="application/x-tex">key => </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mord mathnormal" style="margin-right:0.03588em;">ey</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=></span></span></span></span>val) { echo "$key = $val\n"; } ?>
The above example will output:
d = lemon c = apple b = banana a = orange
See Also
- sort() - Sort an array in ascending order
- ksort() - Sort an array by key in ascending order
- The comparison of array sorting functions
Found A Problem?
There are no user contributed notes for this page.