PHP实现无限极分类
public function getTree($data, $pId=0){ $tree = array(); foreach($data as $k => $v) { if($v['pid'] == $pId) { $v['children'] = $this->getTree($data, $v['id']); $tree[] = $v; } } return $tree; }
处理前:
array (size=4) 0 => array (size=4) 'id' => int 1 'pid' => int 0 'title' => string '首页' (length=6) 'url' => string 'index/index/index' (length=17) 1 => array (size=4) 'id' => int 2 'pid' => int 0 'title' => string '系统设置' (length=12) 'url' => string 'index/sys/setting' (length=17) 2 => array (size=4) 'id' => int 3 'pid' => int 2 'title' => string '角色设置' (length=12) 'url' => string '' (length=0) 3 => array (size=4) 'id' => int 4 'pid' => int 2 'title' => string '用户设置' (length=12) 'url' => string '' (length=0)
处理后:
array (size=2) 0 => array (size=5) 'id' => int 1 'pid' => int 0 'title' => string '首页' (length=6) 'url' => string 'index/index/index' (length=17) 'children' => array (size=0) empty 1 => array (size=5) 'id' => int 2 'pid' => int 0 'title' => string '系统设置' (length=12) 'url' => string 'index/sys/setting' (length=17) 'children' => array (size=2) 0 => array (size=5) 'id' => int 3 'pid' => int 2 'title' => string '角色设置' (length=12) 'url' => string '' (length=0) 'children' => array (size=0) empty 1 => array (size=5) 'id' => int 4 'pid' => int 2 'title' => string '用户设置' (length=12) 'url' => string '' (length=0) 'children' => array (size=0) empty
慢慢来才是最快的