PHP krsort() function

PHP key() function
PHP ksort() function

In this article, you will learn how to sort an associative array in descending order according to the keys. The krsort() function returns an array in which the elements of the input associative array are sorted in descending order according to their keys.

What is the syntax of the krsort() function in PHP?

krsort(array, sorttype)
ParameterDescription
arrayRequired. Specifies the array to sort
sorttypeOptional. Specifies how to compare the array elements/items. 
SORT_STRING (default) – Compare elements as strings
SORT_REGULAR – Compares normally without changing the type
SORT_NUMERIC – Compare as numeric values
SORT_LOCALE_STRING – Compare as a string according to the current locale
PHP krsort function

Examples of krsort() function

Example 1. Sort the associative array according to keys.

<?php
$age=array("Jawad"=>"24","Ahmad"=>"25","Sumerina"=>"39");
krsort($age);
?>
PHP key() function
PHP ksort() function

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top