php 无线分类 根据子级找父级
public function transform($item){ //查询数据 $position = AdminAccess::where('id',$item)->first(); $relation = []; $relation[] = $position; $pid = $position->pid; //循环子级pid,查找所有父级id while ($top = AdminAccess::where(['id' => $pid])->first()) { $pid = $top->pid; //从头部添加数组元素,与 array_push 相似,一个是头部一个是尾部 array_unshift($relation, $top); } foreach ($relation as $k => $v){ $data[] = $v->id ; } return $data; }