PHP Array krsort() Function
Last Updated : 17 Mar 2025
The PHP krsort( ) function is used to sort the array elements by the key in descending order. It is mainly used in the associative array. This function was introduced in PHP 4.0.
Syntax
Parameter
| Parameter | Description | Is compulsory |
|---|---|---|
| array | Specifies the array to use. | compulsory |
| array | It specifies how to sort the array values. Possible values - SORT_REGULAR - Default. Treat values as they are (don't change types) SORT_NUMERIC - Treat values numerically SORT_STRING - Treat values as strings SORT_LOCALE_STRING - Treat values as strings, based on local settings. | Optional |
Returns
The krsort( ) function returns true on success or false on failure.
Example 1
Output:
Array
(
[punjab] =>chandigarh
[karnataka] =>bangalore
[jharkhand] =>ranchi
[goa] =>panaji
[bihar] =>patna
)
Example 2
Output:
Array
(
[d] => volleyball
[c] => hockey
[b] => football
[a] => cricket
)
Example 3
Output:
Array
(
[z] =>php
[y] => java
[x] => html
)
Example 4
Output:
Array
(
[virat] => 29
[sachin] => 45
[ganguly] => 46
)
Next TopicPhp-array-ksort-function