递归
封装递归方法
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 ?>