Home >>PHP Array Functions >PHP krsort() Function
PHP krsort() function is used to sort any given input array by its key in reverse order according to its index values. It sorts in such a way that the relation between indices and values is maintained. It accepts two parameters $array and $sort type. It returns a Boolean value True on success and False on failure.
Syntax:
krsort($array, $sorttype);
Parameter | Description |
---|---|
array | This is a required parameter. It defines the array to sort. |
sorttype | This is an optional parameter. It defines how to compare the array elements. |
Here is an example of krsort() function in PHP:
<html> <body> <?php $a=array("A"=>"53","B"=>"73","C"=>"76"); krsort($a); foreach($a as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?> </body> </html>
Example 2:
<html> <body> <?php $a=array("Ankit"=>"19200","Shyam"=>"21500","Kundan"=>"23000","Ravi"=>"15000"); krsort($a); foreach($a as $x=>$x_value) { echo "Name= " . $x . ", Salary=" . $x_value; echo "<br>"; } ?> </body> </html>