php递归

生成Thinkphp+layui的 菜单所需json数据 

public function menu() { $menu = Db::table('system_menu')->field(['id', 'title', 'icon', 'url'])->where('pid', 0)->select(); $tree = $this->createTree($menu); return json_encode($tree, JSON_UNESCAPED_UNICODE); } public function createTree($list) { $tree = array(); foreach ($list as $k => $v) { $l = Db::table('system_menu')->field(['id', 'title', 'icon', 'url'])->where('pid', $v['id'])->select(); if (!empty($l)) { $v['children'] = $this->createTree($l); } $tree[] = $v; } return $tree; }


生成结果:

[
{
"id": 2,
"title": "系统管理",
"icon": "",
"url": "#",
"children": [
{
"id": 4,
"title": "系统配置",
"icon": "",
"url": "#",
"children": [
{
"id": 3,
"title": "后台首页",
"icon": "fa fa-fw fa-tachometer",
"url": "admin/index/main"
},
{
"id": 5,
"title": "网站参数",
"icon": "fa fa-apple",
"url": "admin/config/index"
},
{
"id": 6,
"title": "文件存储",
"icon": "fa fa-hdd-o",
"url": "admin/config/file"
}
]
},
{
"id": 20,
"title": "系统权限",
"icon": "",
"url": "#",
"children": [
{
"id": 9,
"title": "操作日志",
"icon": "glyphicon glyphicon-console",
"url": "admin/log/index"
},
{
"id": 19,
"title": "权限管理",
"icon": "fa fa-user-secret",
"url": "admin/auth/index"
},
{
"id": 21,
"title": "系统菜单",
"icon": "glyphicon glyphicon-menu-hamburger",
"url": "admin/menu/index"
},
{
"id": 22,
"title": "节点管理",
"icon": "fa fa-ellipsis-v",
"url": "admin/node/index"
},
{
"id": 29,
"title": "系统用户",
"icon": "fa fa-users",
"url": "admin/user/index"
}
]
}
]
},
{
"id": 61,
"title": "微信管理",
"icon": "",
"url": "#",
"children": [
{
"id": 62,
"title": "微信对接配置",
"icon": "",
"url": "#",
"children": [
{
"id": 63,
"title": "微信接口配置\r\n",
"icon": "fa fa-usb",
"url": "wechat/config/index"
},
{
"id": 64,
"title": "微信支付配置",
"icon": "fa fa-paypal",
"url": "wechat/config/pay"
}
]
},
{
"id": 65,
"title": "微信粉丝管理",
"icon": "",
"url": "#",
"children": [
{
"id": 66,
"title": "粉丝标签",
"icon": "fa fa-tags",
"url": "wechat/tags/index"
},
{
"id": 67,
"title": "已关注粉丝",
"icon": "fa fa-wechat",
"url": "wechat/fans/index"
},
{
"id": 86,
"title": "粉丝黑名单",
"icon": "fa fa-reddit-alien",
"url": "wechat/fans/back"
}
]
},
{
"id": 68,
"title": "微信订制",
"icon": "",
"url": "#",
"children": [
{
"id": 69,
"title": "微信菜单定制",
"icon": "glyphicon glyphicon-phone",
"url": "wechat/menu/index"
},
{
"id": 70,
"title": "关键字管理",
"icon": "fa fa-paw",
"url": "wechat/keys/index"
},
{
"id": 71,
"title": "关注自动回复",
"icon": "fa fa-commenting-o",
"url": "wechat/keys/subscribe"
},
{
"id": 81,
"title": "无配置默认回复",
"icon": "fa fa-commenting-o",
"url": "wechat/keys/defaults"
}
]
},
{
"id": 82,
"title": "素材资源管理",
"icon": "",
"url": "#",
"children": [
{
"id": 83,
"title": "添加图文",
"icon": "fa fa-folder-open-o",
"url": "wechat/news/add?id=1"
},
{
"id": 85,
"title": "图文列表",
"icon": "fa fa-file-pdf-o",
"url": "wechat/news/index"
}
]
}
]
}
]

  

 

又修改了下,净化了些:

public function menu()
    {
      
        //生成树状菜单
        $menu = Db::table('system_menu')->field(['id', 'title as name', 'url as href'])->where('pid', 0)->order('sort')->select();
        $tree = $this->createTree2($menu);
        return $tree;  //可以改为返回json

    }


去掉pid里的href
public function createTree2($list)
    {
        $tree = array();
        foreach ($list as $k => $v) {
            $l = Db::table('system_menu')->field(['id', 'title as name', 'url as href'])->where('pid', $v['id'])->order('sort')->select();
            if (!empty($l)) {
                if (isset($v['href'])) {
                    unset($v['href']);
                }
                $v['children'] = $this->createTree2($l);
            }
            $tree[] = $v;
        }
        return $tree;
    }

 

posted @ 2017-08-12 22:16  swocn  阅读(132)  评论(0)    收藏  举报