title: Build hierarchical list from array
body: <p>I have an array of categories that looks something like</p>
  
  <pre><code>array(
    array(
      'id' =&gt; '1',
      'path' =&gt; '1',
      'children_count' =&gt; '16',
      'name' =&gt; 'A'
    ),
    array(
      'id' =&gt; '3',
      'path' =&gt; '1/2/3',
      'children_count' =&gt; '0',
      'name' =&gt; 'C'
    ),
    array(
      'id' =&gt; '2',
      'path' =&gt; '1/2',
      'children_count' =&gt; '9',
      'name' =&gt; 'B'
    ),
    array(
      'id' =&gt; '4',
      'path' =&gt; '1/2/4',
      'children_count' =&gt; '0',
      'name' =&gt; 'D'
    )
  )
  </code></pre>
  
  <p>What I'm trying to build is a hierarchal array (the <code>path</code> key shows the relation based on <code>id</code>), so the output is in the correct order (root parent first, then children, then more children) and indent the <code>name</code> based on how far down in the child is.  The end result should look something like:</p>
  
  <pre><code>A
  -B
  --C
  --D
  </code></pre>
  
  <p>Here's what I have so far, but it's obviously not working</p>
  
  <pre><code>$categoryTree = array();
  
  foreach ($categories as $category) {
  
      $categoryTree[(string)$category['path']] = str_repeat("-", substr_count($category['path'], "/")) . $category['name'];
  }
  
  ksort($categoryTree);
  
  var_export($categoryTree);
  </code></pre>
  
  <p>Which comes out to:</p>
  
  <pre><code>array (
    '1/2' =&gt; '-B',
    '1/2/3' =&gt; '--C',
    '1/2/4' =&gt; '--D',
    1 =&gt; 'A'
  )
  </code></pre>
  
  <p>How can I get these in the correct order? (and if siblings could be ordered by <code>id</code> that would be dandy too)</p>