Current Location: Home> Latest Articles> PHP krsort sorting combined with multidimensional arrays

PHP krsort sorting combined with multidimensional arrays

gitbox 2025-05-28

What is krsort()?

krsort() is a built-in function in PHP for sorting arrays in reverse order by key names. It sorts the array from large to small according to the alphabetical or numeric order of the key name, and retains the association between the key name and the key value.

grammar:

 bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
  • $array : an array that needs to be sorted, and must be a reference when passed in.

  • $sort_flags : a flag for sorting behavior (optional). Commonly used ones include SORT_REGULAR , SORT_NUMERIC , SORT_STRING , etc.


Features of krsort() for multi-dimensional arrays

When krsort() uses multi-dimensional arrays, it will only sort the first layer key names in reverse order, and the order of the sub-arrays inside will not be affected. If you want to sort the subarrays in reverse order, you need to combine recursive methods to achieve this.


Practical example: Use krsort() for multidimensional arrays

The following example demonstrates how to use krsort() to sort the first layer key names of a multidimensional array in reverse order:

 <?php
// Define a multi-dimensional array
$multiArray = [
    "b" => ["name" => "Zhang San", "age" => 25],
    "a" => ["name" => "Li Si", "age" => 30],
    "c" => ["name" => "Wang Wu", "age" => 22],
];

// usekrsortSorting the first layer key in reverse order
krsort($multiArray);

// Output sorted array
echo "<pre>";
print_r($multiArray);
echo "</pre>";
?>

Output result:

 Array
(
    [c] => Array
        (
            [name] => Wang Wu
            [age] => 22
        )

    [b] => Array
        (
            [name] => Zhang San
            [age] => 25
        )

    [a] => Array
        (
            [name] => Li Si
            [age] => 30
        )
)

From the output, we can see that the key names of the array are successfully arranged in reverse order of alphabetical order c > b > a.


Advanced: Recursively use krsort to sort subarrays in reverse order

If your multidimensional array has a complex structure and needs to sort the subarrays in reverse order, you can use recursive functions:

 <?php
function recursiveKrsort(&$array) {
    if (!is_array($array)) {
        return;
    }
    krsort($array);
    foreach ($array as &$value) {
        if (is_array($value)) {
            recursiveKrsort($value);
        }
    }
}

// Example multidimensional array
$multiArray = [
    "b" => [
        "z" => 1,
        "a" => 2
    ],
    "a" => [
        "c" => 3,
        "b" => 4
    ],
];

// Recursive inverse sorting
recursiveKrsort($multiArray);

echo "<pre>";
print_r($multiArray);
echo "</pre>";
?>

result:

 Array
(
    [b] => Array
        (
            [z] => 1
            [a] => 2
        )

    [a] => Array
        (
            [c] => 3
            [b] => 4
        )
)

You will find that the key names of the first layer and the subarray are sorted in reverse order.


Summarize

  • krsort() is suitable for sorting in reverse order according to key names, and is only valid for the first layer by default.

  • When a multi-dimensional array is to be deeply sorted, it is recommended to call krsort() in conjunction with recursive.

  • Understanding and flexibly using krsort() can help you get twice the result with half the effort when dealing with complex arrays.

Through the examples in this article, I believe you have mastered the method of using krsort() to sort PHP multi-dimensional arrays in reverse order. You are welcome to apply it in practice.