php获取层级数组所有子数组

在mysql的树型结构中

我们可以通过递归将数组显示在一个层级数组中

如何取得一个父级元素的所有子元素

```
//验证token
public function verifyToken(Request $request)
{
$permission_id = $request->permission_id;
$param = $request->all();
$header = ['Authorization:'.$request->header('Authorization')];
$res = ZtService::callCoreApi('account', 'staff/getTokenInfo',$param,$header);
//取指定id的权限
if($permission_id){
$res['all_menu'] = $this->array_find($res['menu'], $permission_id, 'id', 'children');
}
return $this->jsonData($res);
}

public function array_find(array $list, $id, $key = 'id', $child = 'children')
{
foreach ($list as $v) {
if($v[$key]==$id){
$return = $v;
}else{
if(!empty($v[$child])){
$return = $this->array_find($v[$child],$id,$key,$child);
}
}
if(!empty($return)){
break;
}
}
return $return??[];
}

//支持多个id
// public function array_find(array $list, $ids, $key = 'id', $child = 'children',&$new_array=[])
// {

// foreach ($list as $v) {
// if(in_array($v[$key],$ids)){
// $new_array[] = $v;
// }else{
// if(!empty($v[$child])){
// array_find($v[$child],$ids,$key,$child,$new_array);
// }
// }
// }
// return $new_array;
// }

```

    A+
发布日期:2023年09月22日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: