知识学习

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 5510|回复: 0

thinkphp 递归菜单 无限极分类

[复制链接]

升级   100%

152

主题

0

回帖

752

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
752
发表于 2022-7-14 21:40:02 | 显示全部楼层 |阅读模式
  1. /**
  2.      * 获取所有菜单
  3.      * @return array
  4.      * @throws \think\db\exception\DataNotFoundException
  5.      * @throws \think\db\exception\DbException
  6.      * @throws \think\db\exception\ModelNotFoundException
  7.      */
  8.     static public function getMenuAll(){
  9.         $res=self::where('hid',0)->field('id,pid,url,icon,title,sort,group')->order('pid', 'asc')->select()->toArray();
  10.         return self::makeArr($res);
  11.     }
  12.     /**
  13.      * 递归循环
  14.      * @param $res  总数组
  15.      * @param int $pid 父级id
  16.      * @return array
  17.      */
  18.     static  public function makeArr($res, $pid=0){
  19.         $arr = [];
  20.         $item['pid']=$pid;
  21.         $data=self::screen($res,$item);
  22.         foreach ($data as $key=>$val){
  23.             $ite['pid']=$val['id'];
  24.             $result = self::screen($res,$ite);
  25.             if (!empty($result)){
  26.                $val['child']= self::makeArr($res,$val['id']);
  27.             }
  28.             $arr[] = $val;
  29.         }
  30.         return  $arr;
  31.     }

  32.     /**
  33.      * 数组键值对查询 返回查询数组
  34.      * @param $res
  35.      * @param $val
  36.      * @return array
  37.      */
  38.     static public function screen($res,$val){
  39.         return array_filter($res,function ($var) use ($val){
  40.             if($var['pid']==$val['pid']) return true;
  41.         });
  42.     }
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|知识学习 ( 鲁ICP备20015483号 )

GMT+8, 2026-5-13 21:56 , Processed in 0.065300 second(s), 27 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表