class RbacController extends Controller { //rbac第二种方法 public function show() { //查询出数据 $data = DB::SELECT("select * from rbac"); //调用方法 $arr = $this->rbac($data); var_dump($arr); } public function rbac($data,$pid = 0,$level = 0) { //定义数组 static $arr = []; //循环遍历 foreach ($data as $key => $v) { if ($v->pid == $pid) { $v->level = $v->name; $arr[] = $v; unset($data[$key]); //再次调用方法 $this->rbac($data,$v->id,$level+1); } } return $arr; }