多维数组字段排序

文章描述了一个网站后台管理系统的菜单结构,通过递归和冒泡排序算法对菜单进行了按照sort字段升序排列,主要涉及管理员、用户管理、权限管理和首页等模块。
摘要由CSDN通过智能技术生成

有以下多维数组:

$items = [
    [
        "name" =>"首页",
        "desc" =>"",
        "route" =>"home",
        "sort" =>9,
        "menus" =>[]
    ],
    [
        "name" =>"管理员",
        "desc" =>"",
        "route" =>"admin",
        "sort" =>4,
        "menus" =>[
            [
                "name" =>"用户管理",
                "desc" =>"",
                "route" =>"admin/user",
                "sort" =>6,
                "menus" =>[
                    [
                        "name" =>"用 户列表",
                        "desc" =>"",
                        "route" =>"admin/user/list",
                        "sort" =>3,
                        "menus" =>[]
                    ],
                    [
                        "name" =>"创建用户",
                        "desc" =>"",
                        "route" =>"admin/user/create",
                        "sort" =>2,
                        "menus" =>[]
                    ]
                ]
            ],
            [
                "name" =>"权限管理",
                "desc" =>"",
                "route" =>"admin/rule",
                "sort" =>8,
                "menus" =>[
                    [
                        "name" =>"权限列表",
                        "desc" =>"",
                        "route" =>"admin/rule/list",
                        "sort" =>1,
                        "menus" =>[]
                    ]
                ]
            ],
            [
                "name" =>"分组管理",
                "desc" =>"",
                "route" =>"admin/group",
                "sort" =>5,
                "menus" =>[
                    [
                        "name" =>"分组列表",
                        "desc" =>"",
                        "route" =>"admin/gr oup/list",
                        "sort" =>7,
                        "menus" =>[]
                    ],
                    [
                        "name" =>"分组-权限",
                        "desc" =>"",
                        "route" =>"admin/group/rule",
                        "sort" =>11,
                        "menus" =>[
                            [
                                "name" =>"分组-权限:列表",
                                "desc" =>"",
                                "route" =>"admin/group/rule/list",
                                "sort" =>10,
                                "menus" =>[]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
]

要求按sort对该多维数组进行排序

算法函数:递归、冒泡排序

function arrSort(array &$items, $sortField, $subField = null)
{
    if (!($len = count($items))) return;
    if (!is_null($subField)) {
        foreach ($items as &$item) {
            arrSort($item[$subField], $sortField, $subField);
        }
    }
    for ($i = 1; $i < $len; $i++) {
        for ($j = 0; $j < $len - $i; $j++) {
            if ($items[$j][$sortField] > $items[$j + 1][$sortField]) {
                $tmp = $items[$j];
                $items[$j] = $items[$j + 1];
                $items[$j + 1] = $tmp;
            }
        }
    }
}
//排序
arrSort($items, 'sort', 'menus');

排序结果:

[
    [
        "name" =>"管理员",
        "desc" =>"",
        "route" =>"admin",
        "sort" =>4,
        "menus" =>[
            [
                "name" =>"分组管理",
                "desc" =>"",
                "route" =>"admin/group",
                "sort" =>5,
                "menus" =>[
                    [
                        "name" =>"分组列表",
                        "desc" =>"",
                        "route" =>"admin/group/list",
                        "sort" =>7,
                        "menus " =>[]
                    ],
                    [
                        "name" =>"分组-权限",
                        "desc" =>"",
                        "route" =>"admin/group/rule",
                        "sort" =>11,
                        "menus" =>[
                            [
                                "name" =>"分组-权限:列表",
                                "desc" =>"",
                                "route" =>"admin/group/rule/list",
                                "sort" =>10,
                                "menus" =>[]
                            ]
                        ]
                    ]
                ]
            ],
            [
                "name" =>"用户管理",
                "desc" =>" ",
                "route" =>"admin/user",
                "sort" =>6,
                "menus" =>[
                    [
                        "name" =>"创建用户",
                        "desc" =>"",
                        "route" =>"admin/user/create",
                        "sort" =>2,
                        "menus" =>[]
                    ],
                    [
                        "name" =>"用户列表",
                        "desc" =>"",
                        "route" =>"admin/user/list",
                        "sort" =>3,
                        "menus" =>[]
                    ]
                ]
            ],
            [
                "name " =>"权限管理",
                "desc" =>"",
                "route" =>"admin/rule",
                "sort" =>8,
                "menus" =>[
                    [
                        "name" =>"权限列表",
                        "desc" =>"",
                        "route" =>"admin/rule/list",
                        "sort" =>1,
                        "menus" =>[]
                    ]
                ]
            ]
        ]
    ],
    [
        "name" =>"首页",
        "desc" =>"",
        "route" =>"home",
        "sort" =>9,
        "menus" =>[]
    ]
]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值