title: php convert flat family list to tree
body: <p>this one is a little Q&amp;A-style...</p>
  
  <p>i've been searching for this situation and couldn't find any solution, so i build my own.  </p>
  
  <p>it took me a few days to get a working code for my needs, but now i am wondering if there's a more efficient way:</p>
  
  <pre><code>$data = [
    ['a','aa','aaa'],
    ['a','aa','aab'],
    ['a','aa','aac'],
    ['a','ab'],
    ['a','ac','aca'],
    ['b','ba'],
    ['b','bb'],
    ['b','bc'],
  ];
  
  
  function tree($values){
    $on_end = sizeof($values) == 1 &amp;&amp; sizeof($values[0]) == 0;
    if ( $on_end ) return null;
  
    $tree1 = [];
    $tree2 = [];
  
    foreach ($values as $a){
      $parent = $a[0];
      //remove the first column
      array_shift($a);
      $tree1[ $parent ][] = $a;
    }
  
    foreach ($tree1 as $parent =&gt; $b){
      $tree2[ $parent ] = tree( $b );
  
    }
  
    return $tree2;
  
  }
  
  
  echo "&lt;pre&gt;".print_r(tree($data),true)."&lt;/pre&gt;";
  </code></pre>
  
  <p>this is the result:</p>
  
  <pre><code>Array
  (
  [a] =&gt; Array
      (
          [aa] =&gt; Array
              (
                  [aaa] =&gt; 
                  [aab] =&gt; 
                  [aac] =&gt; 
              )
  
          [ab] =&gt; 
          [ac] =&gt; Array
              (
                  [aca] =&gt; 
              )
  
      )
  
  [b] =&gt; Array
      (
          [ba] =&gt; 
          [bb] =&gt; 
          [bc] =&gt; 
      )
  
  )
  </code></pre>
  
  <p>if anybody got a better one, please post it!</p>