zoukankan      html  css  js  c++  java
  • 无限极分类,子集跟着父集排列,用于后台显示菜单管理

    在Home模块下建一个Clas文件夹,里面建一个Cate.class.php类文件,类文件代码:

    <?php 
    namespace HomeClas;
    class Cate{
        static public function unlimitedForLevel($data,$html='|__',$pid=0,$level=0){
            $arr=array();
            foreach ($data as $v) {
                if ($v['pid']==$pid) {
                    $v['level']=$level+1;
                    $v['html']=str_repeat($html, $level);
                    $arr[]=$v;
                    $arr=array_merge($arr,self::unlimitedForLevel($data,$html,$v['id'],$level+1));
                }
            }
            return $arr;
        }
    }
    
     ?>

    php代码如下:

    <?php
    namespace HomeController;
    use ThinkController;
    use HomeClasCate;
    class IndexController extends Controller {
        public function index(){
            $data=array(
                    '0'=>array(
                            'id'=>1,
                            'pid'=>0
                        ),
                    '1'=>array(
                            'id'=>2,
                            'pid'=>0
                        ),
                    '2'=>array(
                            'id'=>3,
                            'pid'=>1
                        ),
                    '3'=>array(
                            'id'=>4,
                            'pid'=>3
                        )
                );
            $data=Cate::unlimitedForLevel($data);
            dump($data);die;
            $this->display();
                }
    }

    效果如下:

  • 相关阅读:
    HDU 3068 Manacher
    HDU 6188最小费用流
    Codeforces Round #442 (Div. 2) Danil and a Part-time Job
    并查集
    HDU 5988最小网络流(浮点数)
    HOJ
    HOJ
    POJ
    POJ
    关于async
  • 原文地址:https://www.cnblogs.com/hltswd/p/5118464.html
Copyright © 2011-2022 走看看