递归(layui树形组件数据)

本文介绍如何将数据库中获取的权限菜单数据转换为layui树形组件所需格式,通过递归算法处理,实现数据的层级展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

layui树形组件需要下列形式的数据。处理数据库数据的时候用到递归,但是有一点点变化。
在这里插入图片描述
原数据:

Array
(
    [0] => Array
        (
            [id] => 23
            [name] => admin/article/lst
            [title] => 文档
            [status] => 1
            [condition] => 
            [type] => 1
            [pid] => 0
            [show] => 1
        )

    [1] => Array
        (
            [id] => 22
            [name] => admin/ad/add
            [title] => 广告添加
            [status] => 1
            [condition] => 
            [type] => 1
            [pid] => 18
            [show] => 1
        )

    [2] => Array
        (
            [id] => 20
            [name] => admin/ad/del/1
            [title] => 广告删除1
            [status] => 1
            [condition] => 
            [type] => 1
            [pid] => 19
            [show] => 1
        )

    [3] => Array
        (
            [id] => 19
            [name] => admin/ad/del
            [title] => 广告删除
            [status] => 1
            [condition] => 
            [type] => 1
            [pid] => 18
            [show] => 1
        )

    [4] => Array
        (
            [id] => 18
            [name] => admin/ad/lst
            [title] => 广告
            [status] => 1
            [condition] => 
            [type] => 1
            [pid] => 0
            [show] => 1
        )

    [5] => Array
        (
            [id] => 14
            [name] => admin/admin
            [title] => 管理员
            [status] => 0
            [condition] => 
            [type] => 1
            [pid] => 0
            [show] => 1
        )

    [6] => Array
        (
            [id] => 15
            [name] => admin/admin/add
            [title] => 管理员添加
            [status] => 1
            [condition] => 
            [type] => 1
            [pid] => 14
            [show] => 1
        )

    [7] => Array
        (
            [id] => 16
            [name] => admin/admin/del
            [title] => 管理员删除
            [status] => 1
            [condition] => 
            [type] => 1
            [pid] => 14
            [show] => 1
        )

    [8] => Array
        (
            [id] => 24
            [name] => admin/article/add
            [title] => 文档添加
            [status] => 1
            [condition] => 
            [type] => 1
            [pid] => 23
            [show] => 1
        )

    [9] => Array
        (
            [id] => 25
            [name] => admin/article/del
            [title] => 文档删除
            [status] => 1
            [condition] => 
            [type] => 1
            [pid] => 23
            [show] => 1
        )

)

php代码:

 public function getShu()
    {
        $rules = self::order('id', 'desc')->select();
        $re = $this->_getShu($rules);
        return $re;
    }

    public function _getShu($data, $pid = 0)
    {
        $t = [];
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $str['title'] = $v['title'];
                $str['id'] = $v['id'];
                $str['children'] = $this->_getShu($data, $v['id']);
                if (empty($str['children'])) {
                    unset($str['children']);
                }
                $t[] = $str;
            }
        }
        return $t;
    }

处理后得到的数据:

Array
(
    [0] => Array
        (
            [title] => 文档
            [id] => 23
            [children] => Array
                (
                    [0] => Array
                        (
                            [title] => 文档删除
                            [id] => 25
                        )

                    [1] => Array
                        (
                            [title] => 文档添加
                            [id] => 24
                        )

                )

        )

    [1] => Array
        (
            [title] => 广告
            [id] => 18
            [children] => Array
                (
                    [0] => Array
                        (
                            [title] => 广告添加
                            [id] => 22
                        )

                    [1] => Array
                        (
                            [title] => 广告删除
                            [id] => 19
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => 广告删除1
                                            [id] => 20
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [title] => 管理员
            [id] => 14
            [children] => Array
                (
                    [0] => Array
                        (
                            [title] => 管理员删除
                            [id] => 16
                        )

                    [1] => Array
                        (
                            [title] => 管理员添加
                            [id] => 15
                        )

                )

        )

)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值