title: Sorting data into mulit-dimensional, hierarchical array with PHP
body: <p>I need to sort an array of objects hierarchically. The data looks like this:</p>
  
  <pre><code>array (size=54)
    0 =&gt; 
      object(stdClass)[786]
        public 'term_id' =&gt; string '1' (length=3)
        public 'name' =&gt; string 'Boots' (length=25)
        public 'parent' =&gt; string '0' (length=3)
    1 =&gt; 
      object(stdClass)[785]
        public 'term_id' =&gt; string '2' (length=3)
        public 'name' =&gt; string 'Dresses' (length=25)
        public 'parent' =&gt; string '1' (length=3)
    2 =&gt; 
      object(stdClass)[786]
        public 'term_id' =&gt; string '3' (length=3)
        public 'name' =&gt; string 'Scarves' (length=25)
        public 'parent' =&gt; string '2' (length=3)
    3 =&gt; 
      object(stdClass)[785]
        public 'term_id' =&gt; string '4' (length=3)
        public 'name' =&gt; string 'Gloves' (length=25)
        public 'parent' =&gt; string '1' (length=3)
  </code></pre>
  
  <p>I want to create a multidimensional array that would show this hierarchy of "parent and children". The <code>parent</code> property of each object refers to the <code>term_id</code> of another object.</p>
  
  <p>The result would look something like this:</p>
  
  <pre><code>array (size=54)
    0 =&gt; 
      object(stdClass)[786]
        public 'term_id' =&gt; string '1' (length=3)
        public 'name' =&gt; string 'Boots' (length=25)
        public 'parent' =&gt; string '0' (length=3)
        public 'children' =&gt; array (size=2)
              0 =&gt; 
                  object(stdClass)[785]
                    public 'term_id' =&gt; string '2' (length=3)
                    public 'name' =&gt; string 'Dresses' (length=25)
                    public 'parent' =&gt; string '1' (length=3)
                    public 'children' =&gt; (size=1)
                        0 =&gt; 
                          object(stdClass)[786]
                            public 'term_id' =&gt; string '3' (length=3)
                            public 'name' =&gt; string 'Scarves' (length=25)
                            public 'parent' =&gt; string '2' (length=3)
              1 =&gt;
                  object(stdClass)[785]
                    public 'term_id' =&gt; string '4' (length=3)
                    public 'name' =&gt; string 'Gloves' (length=25)
                    public 'parent' =&gt; string '1' (length=3)      
  </code></pre>
  
  <p>So far I have come up with this code:</p>
  
  <pre><code>$sortedCategories = array();
  foreach($shopCategories as $shopCategory) {       
      $tmp = $shopCategory;
      foreach($shopCategories as $category) {
          if ($tmp-&gt;term_id == $category-&gt;parent) {
              $tmp-&gt;children[] = $category;
              $sortedCategories[] = $tmp;
          } 
      }
  }
  </code></pre>
  
  <p>,but I cannot get it work with the multi-level hierarchy.</p>
  
  <p>How can I sort the data to achieve the desired result?</p>