In PHP, krsort() is a very common sorting function used to sort arrays in reverse order of key names. Many developers may have a question when using krsort() : Will the key names of the PHP array be re-indexed after sorting? In this article, we will discuss this issue in detail and understand the rules behind krsort() .
The function of the krsort() function is to sort the array in reverse order of the array key names. It retains the mapping relationship between the key name and the corresponding value and supports various sorting flags. The basic usage methods are as follows:
<?php
$array = [
"apple" => 3,
"banana" => 2,
"cherry" => 5
];
krsort($array);
print_r($array);
?>
After running the above code, the output array will be sorted in reverse order of the key names:
Array
(
[cherry] => 5
[banana] => 2
[apple] => 3
)
As you can see, the array is arranged in reverse order according to the key names.
An important feature of krsort() sorting is that it does not re-index the key names of the array. This means that if the key names in the array are discontinuous, the key names of the array will remain as they are after krsort() sort. In other words, krsort() simply sorts the key names of the array inverse order and does not change the type of the key names or their original order.
<?php
$array = [
1 => "apple",
3 => "banana",
2 => "cherry"
];
krsort($array);
print_r($array);
?>
The output result is:
Array
(
[3] => banana
[2] => cherry
[1] => apple
)
In this example, although the key names of the array are arranged in reverse order, the key names 1 , 2 , 3 remain unchanged and are not reindexed.
The key names of PHP arrays can be of integer or string type. When you sort an array using krsort() , it does not rearrange these key names, but simply arranges them in reverse order by the value of the key name . PHP sorting functions, such as krsort() , do not change the key name of the array by default unless you explicitly ask for a reindex.
If you want to reconstruct consecutive integer key names after sorting the array, you can use the array_values() function, which will discard all key names in the array and regenerate the index from 0 :
<?php
$array = [
1 => "apple",
3 => "banana",
2 => "cherry"
];
krsort($array);
$array = array_values($array);
print_r($array);
?>
The output result is:
Array
(
[0] => banana
[1] => cherry
[2] => apple
)
This way you get a reindexed array with the key names starting at 0 .
The krsort() function also supports an optional sort flag parameter. This parameter allows you to control the sorting method more carefully, such as using SORT_STRING to sort dictionary order, or using SORT_NUMERIC to sort numerical order. Common usages are as follows:
<?php
$array = [
"10" => "apple",
"2" => "banana",
"30" => "cherry"
];
krsort($array, SORT_NUMERIC);
print_r($array);
?>
The output result is:
Array
(
[30] => cherry
[10] => apple
[2] => banana
)
In this example, we sort the key names in reverse order by numeric size by SORT_NUMERIC .
When sorting with krsort() , the key names of the PHP array are not re-indexed. krsort() simply arranges the array in reverse order based on the value of the key name, and the key names in the array remain unchanged. If you want to get a continuous, reindexed array, you can use the array_values() function. After understanding these rules, you can more flexibly control the sorting of PHP arrays and the processing of key names.
If you have any questions or need more examples, feel free to ask.