递归

封装递归方法

 public function recursion($data,$pid = 0 ,$level = 0)
        {
            static $str = [];
            foreach ($data as $key => $val)
        {
            if($val['pid'] == $pid)
            {
                $val['level'] = $level;
                $str[] = $val;
                $this->recursion($data,$val['id'],$level+1);
            }
        }
        return $str;
    }

controller:

   //tp框架 
    public function  hh(){
        $res=M('digui')->select();
        $re=D('User')->recursion($res);
        
        $this->assign('res',$re);
        $this->display("hh");

    }    

view

    <?php foreach ($res as $key => $val): ?>
            
                <tr>
                    <td><?php echo str_repeat('--',$val['level']).$val['title'];?></td>
                </tr>
                
        <?php endforeach ?>

 

posted @ 2017-06-08 20:10  穷帅哥依然纵横一方  阅读(131)  评论(0编辑  收藏  举报