1. 程式人生 > >樹狀無限極分類getTree tree

樹狀無限極分類getTree tree

樹狀無限極分類   提高2種方式
function getTree($list,$id=0,$level=0) {
    static $tree = array();
    foreach($list as $row) {
        if($row['pid']==$id) {
            $row['level'] = $level;
            $row['name'] = str_repeat('----',$level). $row['name'];
            $tree[] = $row;
            $this->getTree($list, $row['id'], $level + 1);
        }
    }
    return $tree;
}

function getTree($list,$pid=0,$level=0) {
    static $tree = array();
    foreach($list as $row) {
        if($row['dept_pid']==$pid) {
            $row['dept_level'] = $level;
            $tree[] = $row;
            getTree($list, $row['dept_id'], $level + 1);
        }
    }
    return $tree;
}