在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(); } }
效果如下: