我有用戶的數組,每個用戶都可以有父母:如何在PHP中創建樹視圖?
array(6) {
guest => NULL
moderator => NULL
member => array(1) [ //username
0 => "guest" (5) //parent
]
user => array(1) [
0 => "member" (6)
]
admin => array(1) [
0 => "moderator" (9)
]
}
而且我要讓從這個數據樹視圖。結果應該是這樣的:http://www.phorum.org/phorum5/file.php/62/3237/treeview_lightweight_admin_screenshot.jpg
結果將是:
- guest
- member
- user
- moderator
- admin
編輯:
我試着寫樹生成器,但我不知道如何使用遞歸。
private function generateTree($node)
{
$return = array();
if(is_array($node))
{
foreach($node as $user => $parents)
{
if(is_null($parents))
{
$return[$user] = null;
}
if(is_array($parents))
{
foreach($parents as $parent)
{
if(array_key_exists($parent, $return))
{
$return[$parent] = $user;
}
else
{
dump($user, $parent);
}
}
}
}
}
return $return;
}
回報:
array(2) {
guest => "member" (6)
moderator => "admin" (5)
}
左:
user => array(1) [
0 => "member" (6)
]
你幾乎已經描述了該怎麼做:打印出數組recursevly並添加CSS樣式。 – Peon 2012-07-13 13:04:06