Current Location: Home> Latest Articles> How to Sort Keys of a Multidimensional Nested Array in Reverse Order Using krsort in PHP?

How to Sort Keys of a Multidimensional Nested Array in Reverse Order Using krsort in PHP?

gitbox 2025-09-12
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// This part is unrelated to the PHP code</span></span><span>
</span><span><span class="hljs-comment">// For example, here could be some initialization, declarations, or header outputs</span></span><span>
<p></span>echo "This part is not related to the main content\n";</p>
<p>?></p>
<p><hr></p>
<p><?php<br>
<span class="hljs-comment">/**</p>
<ul data-is-only-node="" data-is-last-node="">
<li>
<p>Title: How to Sort Keys of a Multidimensional Nested Array in Reverse Order Using krsort in PHP?</p>
</li>
<li></li>
<li>
<p>In PHP, krsort() is a commonly used array function that sorts an array in reverse order by <strong>key</strong> (from largest to smallest),</p>
</li>
<li>
<p>while preserving the key-value relationships. This is useful for sorting data structures that need to be processed in descending order based on the keys.</p>
</li>
<li></li>
<li>
<h3>1. Basic Usage of krsort</h3>
</li>
<li>
  • $arr = [

  • "c" =&gt; 3,
    
  • "a" =&gt; 1,
    
  • "b" =&gt; 2
    
  • ];

  • krsort($arr);

  • print_r($arr);

  • The result will be:

  • Array

  • (

  • [c] =&gt; 3
    
  • [b] =&gt; 2
    
  • [a] =&gt; 1
    
  • )

  • 2. Case of Multidimensional Arrays

  • Suppose we have a multidimensional nested array:

  • $data = [

  • "group3" =&gt; [
    
  •     "c" =&gt; 30,
    
  •     "a" =&gt; 10,
    
  •     "b" =&gt; 20
    
  • ],
    
  • "group1" =&gt; [
    
  •     "x" =&gt; 100,
    
  •     "z" =&gt; 300,
    
  •     "y" =&gt; 200
    
  • ],
    
  • "group2" =&gt; [
    
  •     "foo" =&gt; "bar",
    
  •     "baz" =&gt; "qux"
    
  • ]
    
  • ];

  • If we only want to sort the top-level keys in reverse order:

  • krsort($data);

  • print_r($data);

  • The top-level keys will be sorted as group3, group2, group1.

  • 3. Recursive Sorting of Multidimensional Arrays

  • If we want to sort not only the top-level keys in reverse order but also recursively sort each sub-array's keys, we need to write a recursive function:

  • function recursiveKrsort(array &$array) {

  • // First, reverse sort the current level's keys
    
  • krsort($array);
    
  • // Iterate through each item and if the value is still an array, call the function recursively
    
  • foreach ($array as &amp;$value) {
    
  •     if (is_array($value)) {
    
  •         recursiveKrsort($value);
    
  •     }
    
  • }
    
  • }

  • // Example usage

  • recursiveKrsort($data);

  • print_r($data);

  • This way, all levels of the array will be sorted by keys in reverse order.

  • 4. Summary

    • Using krsort() can easily sort the keys of a one-dimensional array in reverse order.

    • For multidimensional arrays, we can use a recursive function to process each level and perform a global key sorting in reverse order.

    • This approach is suitable for scenarios where data, such as configuration settings or grouped data, needs to be displayed in reverse order based on the keys.
      */
      ?>