PHP krsort() Function

The krsort() function sorts an associative array in descending order, according to the key.

Tip: Use the ksort() function to sort an associative array in ascending order, according to the key.

Tip: Use the arsort() function to sort an associative array in descending order, according to the value.

ParameterDescription
arrayRequired. Specifies the array to sort
sortingtypeOptional. Specifies how to compare the array elements/items. Possible values:

Syntax

krsort(array,sortingtype);

Example

$age=array("Alex"=>"35","Cooper"=>"37","Samuel"=>"43");
krsort($age);
print_r($age);

Output

Array ( [Samuel] => 43 [Cooper] => 37 [Alex] => 35 )

Leave a Reply

Your email address will not be published. Required fields are marked *