refactor: 修改用户菜单权限输出结构

This commit is contained in:
2025-03-03 17:53:20 +08:00
parent fd2d246736
commit f9e5f6ceb2
2 changed files with 18 additions and 9 deletions

View File

@@ -10,13 +10,16 @@ if (!function_exists('array_to_tree')) {
* @param int $level 层级
* @return array
*/
function array_to_tree(array $data, int $pid, string $with = 'pid', int $level = 1)
function array_to_tree(array $data, int $pid, string $with = 'pid', int|false $level = 1)
{
$ret = [];
foreach ($data as $item) {
if ($item[$with] == $pid) {
$item['level'] = $level;
$children = array_to_tree($data, $item['id'], $with, $level + 1);
if ($level !== false) {
$item['level'] = $level;
$level = $level + 1;
}
$children = array_to_tree($data, $item['id'], $with, $level);
if ($children) {
$item['children'] = $children;
}