<?php
//$pages = mysql_query('SELECT id, parent, slug FROM pages')
$pages = array(
0 => array('id' => 0, 'parent' => NULL, 'slug' => 'page1'),
1 => array('id' => 1, 'parent' => 0, 'slug' => 'page2'),
2 => array('id' => 2, 'parent' => 1, 'slug' => 'page3'),
3 => array('id' => 3, 'parent' => 2, 'slug' => 'page4'),
4 => array('id' => 4, 'parent' => 2, 'slug' => 'page5'),
5 => array('id' => 5, 'parent' => 3, 'slug' => 'page6'),
6 => array('id' => 6, 'parent' => 1, 'slug' => 'page7'),
7 => array('id' => 7, 'parent' => 1, 'slug' => 'page8'),
8 => array('id' => 8, 'parent' => 3, 'slug' => 'page9'),
9 => array('id' => 9, 'parent' => 7, 'slug' => 'page10'),
10 => array('id' => 10, 'parent' => 5, 'slug' => 'page11'),
);
foreach($pages as &$page) {
if(!is_null($page['parent'])) {
$pages[$page['parent']]['childs'][$page['id']] = &$page;
}
}
foreach($pages as $id => &$page) {
if(!is_null($page['parent'])) {
unset($pages[$id]);
}
}
function hierarchy($pages) {
echo '<ul>';
foreach($pages as $page) {
echo '<li>';
echo $page['slug'];
if(!empty($page['childs'])) {
hierarchy($page['childs']);
}
echo '</li>';
}
echo '</ul>';
}
hierarchy($pages);