php递归数组扁平处理

发布时间:2023-09-14 23:17:12 阅读:570次

php递归数组扁平处理

如何将一个递归数组,就是带有层级显示的数组转换成同一层级

比如一个数组是一个树型,如何转换成一个二维数组

以下为代码

public function ssb($aa){
        $sb = array();
        foreach($aa as $v){
            $arr = array();
            $arr['title'] = $v['title'];
            $arr['id'] = $v['id'];
            $arr["pid"] = $v['pid'];
            $arr["icon"] = $v['icon'];
            $arr["path"] = $v['path'];
            $arr["component"] = $v['component'];
            $arr["level"] = $v['level'];
            $arr["params"] = $v['params'];
            $arr["url"] = $v['url'];
            $sb[] = $arr;
            if(isset($v['child'])){
                $sbb = $this->ssb($v['children']);
                $sb = array_merge($sb,$sbb);
            }
        }
        return $sb;
    }

    //验证token
    public function verifyToken(Request $request)
    {
        $param = $request->all();
        // $header = $request->header();
        $header = ['Authorization:'.$request->header('Authorization')];
        $res = ZtService::callCoreApi('account', 'staff/getTokenInfo',$param,$header);
        $menu = $res['menu'];
        if(is_array($menu) && count($menu) > 0){
            foreach($menu as $key=>$value){
                $lis = array();
                $liss['id'] = $value['id'];
                $liss['title'] = $value['title'];
                $liss["pid"] = $value['pid'];
                $liss["icon"] = $value['icon'];
                $liss["path"] = $value['path'];
                $liss["component"] = $value['component'];
                $liss["level"] = $value['level'];
                $liss["params"] = $value['params'];
                $liss["url"] = $value['url'];
                //分解最终用$lis[]存储
                $lis[] = $liss;
                if(isset($value['children'])){
                    $li = $this->ssb($value['children']);
                    $lis = array_merge($lis,$li);
                }
                $result[] = $lis;
            }
            foreach($result as $kk=>$vv){
                foreach($vv as $kkk=>$vvv){
                    $test[] = $vvv;
                }
            }
            $res['all_menu'] = $test;
        }
        return $this->jsonData($res);
    }

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:php递归数组扁平处理 出自老鄢博客 | 欢迎分享