- // While we have nodes left to build, shift the first (alphabetically)
- // node of the list, place it in our groups list and loop through the
- // list of unplaced nodes to find its children. We make a copy to
- // iterate through because we must modify the unplaced nodes list
- // during the loop.
- while (count($roots)) {
- $new_roots = array();
- $current_rows = $rows;
- $root = array_shift($roots);
- $groups[$root['id']] = array($root['prefix'], $root['title']);
-
- // As you find the children, append them to the end of the new set
- // of roots (maintain alphabetical ordering). Also remove the node
- // from the set of unplaced nodes.
- if (is_array($current_rows)) {
- foreach ($current_rows as $key => $row) {
- if ($row['parents'] == $root['id']) {
- $new_roots[] = array(
- 'id' => $row['id'],
- 'prefix' => $groups[$root['id']][0] . $spacer,
- 'title' => $row['title'],
- );
- unset($rows[$key]);
- }
- }
- }