PHP - krsort() Function
The PHP krsort() function is used to sort an associative array in descending order, according to the key. The function has one optional parameter which can be used to specify sorting type. Please see below for more details.
Syntax
krsort(array, sorting_type)
Parameters
array |
Required. Specify the associative array to sort |
sorting_type |
Optional. There are six sorting types which are mentioned below:
Note: The | (bitwise operator) can be used to specify more than one sorting type, for example - 4|5. |
Return Value
Returns TRUE on success and False in failure.
Example:
In the below example, krsort() function to used to sort an associative array called MyArray in descending order, according to the key.
<?php $MyArray = array("Marry"=>25, "John"=>30, "Jo"=>45, "Kim"=>22, "Adam"=>35); krsort($MyArray); print_r($MyArray); ?>
The output of the above code will be:
Array ( [Marry] => 25 [Kim] => 22 [John] => 30 [Jo] => 45 [Adam] => 35 )
❮ PHP Array functions