Home > Backend Development > PHP Tutorial > php array sorting

php array sorting

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-06 13:52:32
Original
1061 people have browsed it

How to sort the field menu_order in the array below? ? ? ? ? ?

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>
Copy after login
Copy after login

)

Reply content:

How to sort the field menu_order in the array below? ? ? ? ? ?

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>
Copy after login
Copy after login

)

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

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

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

Use custom sorting

<code>usort($menu, function($a, $b) {
    return $a['menu_order'] < $b['menu_order'] ? -1 : 1;
});</code>
Copy after login

Customize sorting. Just find the values ​​of the corresponding nodes and compare them.

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template