方法:
/**
* 树形图的菜单列表
* @param $data
* @param int $parent_id
* @return array
*/
function getTree($data, $keyId = 'id', $keyPid = 'pid', $parent_id = 0)
{
$tree = [];
foreach ($data as $k => $v) {
if ($v[$keyPid] == $parent_id) {
$v['children'] = getTree($data, $keyId, $keyPid, $v[$keyId]);
$tree[] = $v;
unset($data[$k]);
}
}
return $tree;
}
调用:
$DepartmentM = getTree($DepartmentM, 'department_id', 'p_id');