分享一个不错的递归方式,效率更高
/**
* @Notes (备注) : 部门递归所有下级
* @Author (作者) : Seven
* @Date (开发时间) : 2021/9/14 15:58
* @Interface (方法名称) : departmentTree
* @param $data array
* @return array
*/
public function departmentTree($data)
{
$items = array();
foreach ($data as $v) {
$items[$v['id']] = $v;
}
$tree = array();
foreach ($items as $k => $item) {
if (isset($items[$item['parent_id']])) {
$items[$item['parent_id']]['level'][] = &$items[$k];
} else {
$tree[] = &$items[$k];
}
}
return $tree;
}