title: Sort MPTT resultset into a multidimensional array PHP
body: <p>I have been experimenting with the Modified Pre-Order Tree Traversal
  Pattern, my test case code is returning the results as expected however I am
  having trouble converting the 2D array into a multi-dimensional array to present it.</p>
  
  <p>Here is an example of a 3 level menu result, I need to convert this into a multi-dimensional array so that I can iterate it in TAL:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [CategoryID] =&gt; 1
              [ParentID] =&gt; 0
              [CategoryName] =&gt; Default Parent
              [lt] =&gt; 1
              [rt] =&gt; 14
              [tree_depth] =&gt; 1
          )
  
      [1] =&gt; Array
          (
              [CategoryID] =&gt; 8
              [ParentID] =&gt; 1
              [CategoryName] =&gt; SysAdmin
              [lt] =&gt; 2
              [rt] =&gt; 7
              [tree_depth] =&gt; 2
          )
  
      [2] =&gt; Array
          (
              [CategoryID] =&gt; 2
              [ParentID] =&gt; 8
              [CategoryName] =&gt; Linux
              [lt] =&gt; 3
              [rt] =&gt; 4
              [tree_depth] =&gt; 3
          )
  
      [3] =&gt; Array
          (
              [CategoryID] =&gt; 3
              [ParentID] =&gt; 8
              [CategoryName] =&gt; Windows
              [lt] =&gt; 5
              [rt] =&gt; 6
              [tree_depth] =&gt; 3
          )
  
      [4] =&gt; Array
          (
              [CategoryID] =&gt; 5
              [ParentID] =&gt; 1
              [CategoryName] =&gt; Code
              [lt] =&gt; 8
              [rt] =&gt; 13
              [tree_depth] =&gt; 2
          )
  
      [5] =&gt; Array
          (
              [CategoryID] =&gt; 6
              [ParentID] =&gt; 5
              [CategoryName] =&gt; PHP
              [lt] =&gt; 9
              [rt] =&gt; 10
              [tree_depth] =&gt; 3
          )
  
      [6] =&gt; Array
          (
              [CategoryID] =&gt; 7
              [ParentID] =&gt; 5
              [CategoryName] =&gt; Perl
              [lt] =&gt; 11
              [rt] =&gt; 12
              [tree_depth] =&gt; 3
          )
  
  )
  </code></pre>
  
  <p>I need to structure the data so that every parent has a 'Children' key which is an array of arrays repeated, with no limitation on the amount of children a parent/child/grandchild can have, the tree_depth key is worked out automatically by the DBMS, so I simply need to alter the structure of the array.</p>
  
  <p>Any pointers greatly appreciated, I have played with usort() and array_walk_recursive to no avail.</p>
  
  <p>Thanks in advance</p>