What is PHP krsort() Function?
If you want to sort keys an associative array in descending order preserving its key-value associations, use krsort() function.
Syntax:
ksort(array, flag)
Parameters:
The Function has 1 required parameter and 1 optional parameter-
array (Required): it specifies an array. Check the example 1.
flag (Optional): It specifies a constant that describes how to compare array items. It has 6 possible values-
- SORT_REGULAR – it is the default value. It doesn’t change data type of elements if there exists mixed data types (ex. string, numbers) and it uses standard PHP comparison rules. Note, it may lead unexpected results. Check example 2.
- SORT_NUMERIC – It compares array keys as numeric. Use this when you want to sort an array that has both numeric string and integer keys. Check example 3.
- SORT_STRING – It compares array keys as strings. Check example 4.
- SORT_LOCALE_STRING – It compares array keys as strings based on currently set locale. You must set the locale using setlocale() function before to get correct result from SERT_LOCALE_STRING flag. Check example 5.
- SORT_NATURAL – It compares array keys as string using natural ordering like a human. So, “version10” comes before “version8”. Check example 6.
- SORT_FLAG_CASE – It compares case-insensitively array keys as string using natural ordering like a human. So, “Version10” comes before “version1”. It can be used using | (bitwise operator) with SORT_NATURAL or SORT_STRING. Check example 7.
Return Values:
This function always returns TRUE. Note: the function doesn’t return the sorted array, instead, it modifies the original array by changing the key order.
Examples:
Example 1:
<pre>
<?php
$languages = array("Markup" => "HTML", "Styling" => "CSS", "Scripting" => "Javascript", "Serverside" => "PHP");
krsort($languages);
print_r($languages);
?>
</pre>
Output:
Array
(
[Styling] => CSS
[Serverside] => PHP
[Scripting] => Javascript
[Markup] => HTML
)
Example 2:
<pre>
<?php
$languages = array("Markup" => "HTML", "Styling" => "CSS", "Scripting" => "Javascript", "Serverside" => "PHP", 8.5 => "PHPVersion", 5 => "HTMLVersion");
krsort($languages, SORT_REGULAR);
print_r($languages);
?>
</pre>
Output:
Array
(
[Styling] => CSS
[Serverside] => PHP
[Scripting] => Javascript
[Markup] => HTML
[8] => PHPVersion
[5] => HTMLVersion
)
Example 3:
<pre>
<?php
$array = array(8 => "PHPVersion", "05" => "HTMLVersion", "5" => "Other", 4 => "CSSVersion", 16 => "JSVersion", "9.5.0" => "MySQLVersion");
krsort($array, SORT_NUMERIC);
print_r($array);
?>
</pre>
Output:
Array
(
[16] => JSVersion
[9.5.0] => MySQLVersion
[8] => PHPVersion
[05] => HTMLVersion
[5] => Other
[4] => CSSVersion
)
Example 4:
<pre>
<?php
$array = array("First" => "500", "Second" => "200", "Third" => "400", 4 => "1001", 5 => "100");
krsort($array, SORT_STRING);
print_r($array);
?>
</pre>
Output:
Array
(
[Third] => 400
[Second] => 200
[First] => 500
[5] => 100
[4] => 1001
)
Example 5:
<pre>
<?php /* The exampla below sorts the Ukrainian strings in ascending order.*/
setlocale(LC_ALL, 'uk_UA.UTF-8', 'uk_UA.utf8', 'uk_UA', 'ukr_UKR', 'ukr');
$array = array("перше" => "один", "друге" => "два", "По-третє" => "три", "Четверте" => "чотири");
krsort($array, SORT_LOCALE_STRING);
print_r($array);
?>
</pre>
Output:
Array
(
[По-третє] => три
[перше] => один
[друге] => два
[Четверте] => чотири
)
Example 6:
<pre>
<?php
$array_standard = array("Version8.5" => "PHPVersion", "Version5" => "HTMLVersion", "Version1" => "CSSVersion", "Version16" => "JSVersion", "Version9.5.0" => "MySQLVersion");
$array_natural = array("Version8.5" => "PHPVersion", "Version5" => "HTMLVersion", "Version1" => "CSSVersion", "Version16" => "JSVersion", "Version9.5.0" => "MySQLVersion");
krsort($array_standard, );
echo "After Standard Sorting:\n";
print_r($array_standard);
krsort($array_natural, SORT_NATURAL);
echo "After Natural Sorting:\n";
print_r($array_natural);
?>
</pre>
Output:
After Standard Sorting:
Array
(
[Version9.5.0] => MySQLVersion
[Version8.5] => PHPVersion
[Version5] => HTMLVersion
[Version16] => JSVersion
[Version1] => CSSVersion
)
After Natural Sorting:
Array
(
[Version16] => JSVersion
[Version9.5.0] => MySQLVersion
[Version8.5] => PHPVersion
[Version5] => HTMLVersion
[Version1] => CSSVersion
)
Example 7:
<pre>
<?php
$version_standard = array("Version8.5" => "PHPVersion", "version5" => "HTMLVersion", "Version1" => "CSSVersion", "Version16" => "JSVersion", "Version9.5.0" => "MySQLVersion");
$version_natural = array("Version8.5" => "PHPVersion", "version5" => "HTMLVersion", "Version1" => "CSSVersion", "Version16" => "JSVersion", "Version9.5.0" => "MySQLVersion");
krsort($version_standard);
echo "After Standard Sorting:\n";
print_r($version_standard);
krsort($version_natural, SORT_NATURAL | SORT_FLAG_CASE);
echo "After Natural case-insensitive Sorting:\n";
print_r($version_natural);
?>
</pre>
Output:
After Standard Sorting:
Array
(
[version5] => HTMLVersion
[Version9.5.0] => MySQLVersion
[Version8.5] => PHPVersion
[Version16] => JSVersion
[Version1] => CSSVersion
)
After Natural case-insensitive Sorting:
Array
(
[Version16] => JSVersion
[Version9.5.0] => MySQLVersion
[Version8.5] => PHPVersion
[version5] => HTMLVersion
[Version1] => CSSVersion
)
Notes on krsort() Function:
- If multiple keys have same values, the function doesn’t change their order. Check example 3. The string key “05” and “5” are compared numerically equal.
- After sorting, the function set the internal pointer to the first element in the array.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP krsort() Function
The krsort() function is a built-in PHP function and part of the PHP’s array functions. It is a quick and effective way to sort array key in descending order preserving the key-value association.