封装递归方法

复制代码
 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 on 2017-06-13 14:44  zzxu  阅读(141)  评论(0)    收藏  举报