php 数组排序
天蓬老师
天蓬老师 2017-04-10 18:10:10
[PHP讨论组]

对下面数组中字段menu_order排序,怎么解??????

Array
(

[menu] => Array
    (
        [0] => Array
            (
                [menu_name] => 首页
                [login_menu_name] => 
                [icon] => glyphicon glyphicon-home
                [menu_href] => /api/1ek2n5gsut/shop/
                [login_menu_href] => /api/1ek2n5gsut/shop/
                [bind_action_attr] => 0
                [menu_order] => 4
            )

        [1] => Array
            (
                [menu_name] => 我要分销
                [login_menu_name] => 分销中心
                [icon] => glyphicon glyphicon-indent-left
                [menu_href] => /api/1ek2n5gsut/distribute/join/
                [login_menu_href] => /api/1ek2n5gsut/distribute/
                [bind_action_attr] => 1
                [menu_order] => 3
            )

        [2] => Array
            (
                [menu_name] => 购物车
                [login_menu_name] => 
                [icon] => glyphicon glyphicon-shopping-cart
                [menu_href] => /api/1ek2n5gsut/shop/cart/
                [login_menu_href] => /api/1ek2n5gsut/shop/cart/
                [bind_action_attr] => 2
                [menu_order] => 1
            )

        [3] => Array
            (
                [menu_name] => 个人中心
                [login_menu_name] => 
                [icon] => glyphicon glyphicon-user
                [menu_href] => /api/1ek2n5gsut/shop/member/
                [login_menu_href] => /api/1ek2n5gsut/shop/member/
                [bind_action_attr] => 0
                [menu_order] => 1
            )

        [4] => Array
            (
                [menu_name] => 分类
                [login_menu_name] => 
                [icon] => glyphicon glyphicon-th-list
                [menu_href] => /api/1ek2n5gsut/shop/3//allcategory/
                [login_menu_href] => /api/1ek2n5gsut/shop/3//allcategory/
                [bind_action_attr] => 0
                [menu_order] => 
            )

    )

[method] => 

)

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(3)
PHP中文网

自定义排序 找到对应节点的值比较就行了吧

巴扎黑
<?php
foreach ($menu as $key => $row) {
    $menu_order[$key] = $row['menu_order'];
}

// 将数据根据menu_order升序排列
// 把 $menu 作为最后一个参数,以通用键排序
array_multisort($menu_order, SORT_ASC, $menu);

参照:http://php.net/manual/ja/function.array-multisort.php

阿神

使用自定义排序

usort($menu, function($a, $b) {
    return $a['menu_order'] < $b['menu_order'] ? -1 : 1;
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号