baji/app/admin/common.php
2025-03-03 14:47:45 +08:00

28 lines
962 B
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// 这是系统自动生成的公共文件
/**
* 无限级分类
* @param $array
* @param int $pid
* @param int $level
* @return array
*/
function scCategory($array, $pid = 0, $level = 0)
{
//声明静态数组,避免递归调用时,多次声明导致数组覆盖
static $list = [];
foreach ($array as $key => $value) {
//第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
if ($value['pid'] == $pid) {
//父节点为根节点的节点,级别为0也就是第一级
$value['level'] = $level;
//把数组放到list中
$list[] = $value;
//把这个节点从数组中移除,减少后续递归消耗
unset($array[$key]);
//开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
scCategory($array, $value['id'], $level + 1);
}
}
return $list;
}