方法:
static public function getChildren($data,$pid){ $arr=array(); foreach ($data as $v) { if ($v['pid']==$pid) { $arr[]=$v; $arr=array_merge($arr,self::getChildren($data,$v['id'])); } } return $arr; }
调用:
<?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); //$data=Cate::unlimitedForLayer($data); //$data=Cate::getParents($data,4); $data=Cate::getChildren($data,1); echo '<pre>'; print_r($data);die; echo '</pre>'; $this->display(); } }
效果: