Fix for Turn a flat taxonomy-tree into a real tree

  1.   $terms = taxonomy_get_tree(9);
  2.  
  3.   // build a map tid->term
  4.   $tree = array();
  5.   foreach ($terms as &$term) {
  6.     $tree[$term->tid] = &$term;
  7.   }
  8.  
  9.   // add child links to every node
  10.   foreach ($terms as &$term) {
  11.     foreach ($term->parents as $parent) {
  12.       if ($parent != 0) {
  13.         $tree[$parent]->children[] = &$term;
  14.       }
  15.     }
  16.   }
  17.  
  18.   // remove non-root terms
  19.   foreach ($tree as $tid => $term) {
  20.     if ($term->parents[0] != 0) {
  21.       unset($tree[$tid]);
  22.     }
  23.   }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.