2016-07-22 21:02:26 2110次浏览 2条回答 0 悬赏 20 金钱

下面的数组,谁能帮我求一下每层[label]所在的深度

即加一下 [depth] => ?

Array
(
    [label] => 权限管理
    [url] => Array
        (
            [0] => /admin/role/index
        )

    [visible] => 1
    [icon] => fa fa-user
    [options] => Array
        (
            [visible] => 1
            [icon] => fa fa-user
        )

    [items] => Array
        (
            [0] => Array
                (
                    [label] => 角色管理
                    [url] => Array
                        (
                            [0] => /admin/role/index
                        )

                    [icon] => fa fa-circle-o
                    [active] => 
                )

            [1] => Array
                (
                    [label] => 路由管理
                    [url] => Array
                        (
                            [0] => /admin/route/index
                        )

                    [icon] => fa fa-circle-o
                    [active] => 
                )

            [2] => Array
                (
                    [label] => 权限管理
                    [url] => Array
                        (
                            [0] => /admin/permission/index
                        )

                    [visible] => 1
                    [icon] => fa fa-user
                    [options] => Array
                        (
                            [visible] => 1
                            [icon] => fa fa-user
                        )

                    [items] => Array
                        (
                            [0] => Array
                                (
                                    [label] => 0001
                                    [url] => Array
                                        (
                                            [0] => /admin/assignment/view
                                        )

                                    [icon] => fa fa-circle-o
                                    [active] => 
                                )

                        )

                    [active] => 
                )

        )

    [active] => 
)
补充于 2016-07-23 12:00
  • 回答于 2016-07-22 22:51 举报

    其实就是树形分层展示

  • 回答于 2016-07-23 20:53 举报

    大概应该是这个样子:

    function initDepth(&$data, $currentDepth) {
        if (!empty($data['label'])) {
            $data['depth'] = $currentDepth;
            if (!empty($data['items'])) {
                $currentDepth++;
                $length = count($data['items'])
                for ($i=0; $i < $length; $i++) {
                    initDepth($data['items'][$i], $currentDepth);
                }
            }
        }
    }
    initDepth($data, 1);
    

    还有一点要吐槽一下,var_dump出来的数据,不能直接使用,写起代码来很费事,还要自己转换,建议以后提问题的时候,给出直接能放到php中使用的数据,可以考虑使用var_export方法输出

    觉得很赞
您需要登录后才可以回答。登录 | 立即注册
okokad
经理

okokad

注册时间:2016-02-29
最后登录:2017-07-01
在线时长:49小时26分
  • 粉丝12
  • 金钱25
  • 威望80
  • 积分1315

热门问题