php 数组排序

WBOY
Lepaskan: 2016-07-06 13:52:32
asal
1013 orang telah melayarinya

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

Array
(

<code>[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] => </code>
Salin selepas log masuk
Salin selepas log masuk

)

回复内容:

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

Array
(

<code>[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] => </code>
Salin selepas log masuk
Salin selepas log masuk

)

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

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

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

使用自定义排序

<code>usort($menu, function($a, $b) {
    return $a['menu_order'] </code>
Salin selepas log masuk

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

Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan