PHP 父子级树状列表
作者:程序员11 时间:2025-12-16 人气:6 QQ交流群\邮箱:1003265987@qq.com
PHP 父子级树状列表
要展示的内容
注意数组里面需要包含 pid、id这几个字段
/**
* 父子级树状列表
*/
if(!function_exists('get_tree_list')){
//引用方式实现 父子级树状结构
function get_tree_list($list){
//将每条数据中的id值作为其下标
$temp = [];
foreach($list as $v){
$v['son'] = [];
$temp[$v['id']] = $v;
}
//获取分类树
foreach($temp as $k=>$v){
$temp[$v['pid']]['son'][] = &$temp[$v['id']];
}
return isset($temp[0]['son']) ? $temp[0]['son'] : [];
}
}查询结果如下:
{
"code": 200,
"msg": "success",
"data": [
{
"id": 1,
"auth_name": "首页",
"pid": 0,
"pid_path": "0",
"auth_c": "",
"auth_a": "",
"is_nav": 1,
"level": 0,
"son": [
{
"id": 90,
"auth_name": "首页",
"pid": 1,
"pid_path": "0_1",
"auth_c": "",
"auth_a": "",
"is_nav": 1,
"level": 1,
"son": [
{
"id": 91,
"auth_name": "首页",
"pid": 90,
"pid_path": "0_1_90",
"auth_c": "index",
"auth_a": "index",
"is_nav": 1,
"level": 2,
"son": []
},
{
"id": 104,
"auth_name": "首页啊",
"pid": 90,
"pid_path": "0_1_90",
"auth_c": "s",
"auth_a": "y",
"is_nav": 1,
"level": 2,
"son": []
}
]
}
]
}
]
}
温馨提示:
欢迎阅读本文章,觉得有用就多来支持一下,没有能帮到您,还有很多文章,希望有一天能帮到您。
- 上一篇:PHP 无限极分类列表
- 下一篇:没有了
