function build_tree(&$modules, &$parents)
{
$tree = array();
foreach ($modules as $row) {
$branch =& $tree;
if ($row['parent_id']) {
// Go through the tree to find our branch
$parent_tree = $parents[$row['module_id']];
foreach ($parent_tree as $id => $value) {
if (!isset($branch[$id]) && isset($branch['child'])) {
$branch =& $branch['child'];
}
$branch =& $branch[$id];
}
$branch =& $branch['child'];
}
$branch[$row['module_id']] = $row;
if (!isset($branch[$row['module_id']]['child'])) {
$branch[$row['module_id']]['child'] = array();
}
}
return $tree;
}