Check out recursion. Let's assume you have class Node, which is what you're trying to get children for. If you set up a member function for Node called, for instance, getChildren(), you can have that function call itself recursively until it's resolved the hierarchy. The details of what you do with the results depend on your situation. In this example, I assume you simply output each child, indented at each level for readability. You will probably instead want to return them in some structure. For simplicity, I also assume that the children Nodes are stored in an array in the class, and that it's already populated -- though you'll probably be accessing the DB.
class Node
{
var $name;
var $children;
function getChildren($level=0)
{
$indent = str_repeat(" ", ($level*3));
$children = $this->children;
for ($i=0; $i<count($children); $i++)
{
print($indent.$name);
$children[$i]->getChildren($level+1);
}
}
}
So for each Node, it'll get the children Nodes. Then for each child Node, it'll print its name (indented based on the current $level) and run the same function on that child.
-Jeremy