Home >>PHP Array Functions >PHP krsort() Function

PHP krsort() Function

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 Values

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>
Output:
Key=C, Value=76
Key=B, Value=73
Key=A, Value=53

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>
Output:
Name= Shyam, Salary=21500
Name= Ravi, Salary=15000
Name= Kundan, Salary=23000
Name= Ankit, Salary=19200

No Sidebar ads