- A+
测试数据如下图所示:
查询出的数据直接调用此方法:
function getTreeTest($data, $pId=0,$level=0) { $tree = ''; foreach($data as $k => $v) { if($v['parent_id'] == $pId) { //父亲找儿子 $v['level'] = $level; $v['children'] = $this->getTreeTest($data, $v['id'],$level+1); $tree[] = $v; } } return $tree; }
得到数据如下:
Array ( [0] => Array ( [id] => 174 [parent_id] => 0 [type] => 2 [status] => 1 [list_order] => 10000 [app] => Admin [controller] => Index [action] => default [param] => [name] => 权限管理 [icon] => [remark] => [ttttttt] => [level] => 0 [children] => Array ( [0] => Array ( [id] => 175 [parent_id] => 174 [type] => 1 [status] => 1 [list_order] => 10000 [app] => Admin [controller] => Rbac [action] => index [param] => [name] => 角色管理 [icon] => [remark] => [ttttttt] => [level] => 1 [children] => ) [1] => Array ( [id] => 176 [parent_id] => 174 [type] => 1 [status] => 1 [list_order] => 10000 [app] => admin [controller] => Menu [action] => index [param] => [name] => 菜单管理 [icon] => [remark] => [ttttttt] => [level] => 1 [children] => ) [2] => Array ( [id] => 179 [parent_id] => 174 [type] => 1 [status] => 1 [list_order] => 10000 [app] => admin [controller] => User [action] => index [param] => [name] => 用户管理 [icon] => [remark] => [ttttttt] => [level] => 1 [children] => ) ) ) [1] => Array ( [id] => 182 [parent_id] => 0 [type] => 1 [status] => 1 [list_order] => 10000 [app] => Admin [controller] => Comment [action] => index [param] => [name] => 编辑 [icon] => [remark] => [ttttttt] => [level] => 0 [children] => Array ( [0] => Array ( [id] => 183 [parent_id] => 182 [type] => 1 [status] => 1 [list_order] => 10000 [app] => Admin [controller] => Comment [action] => index [param] => [name] => 编辑-01 [icon] => [remark] => [ttttttt] => [level] => 1 [children] => Array ( [0] => Array ( [id] => 185 [parent_id] => 183 [type] => 1 [status] => 1 [list_order] => 10000 [app] => Admin [controller] => Comment [action] => index [param] => [name] => 编辑-01-01 [icon] => [remark] => [ttttttt] => 2018-12-25 11:43:03 [level] => 2 [children] => ) ) ) ) )