Php array sorting

Source: Internet
Author: User
How to sort the menu_order field in the following array ?????? Array ({code...}) sorts the menu_order field in the following Array. how can this problem be solved ??????

Array
(

[Menu] => Array ([0] => Array ([menu_name] => homepage [login_menu_name] => [icon] => glyphicon-home [menu_href] =>/ api/1ek2n5gsut/shop/[login_menu_href] =>/api/1ek2n5gsut/shop/[bind_action_attr] => 0 [menu_order] => 4) [1] => Array ([menu_name] => I want to distribute [login_menu_name] => distribution center [icon] => 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] => shopping cart [login_menu_name] => [icon] => 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] => Personal Center [login_menu_name] => [icon] => 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] => classification [login_menu_name] => [icon] => 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] =>

)

Reply content:

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

Array
(

[Menu] => Array ([0] => Array ([menu_name] => homepage [login_menu_name] => [icon] => glyphicon-home [menu_href] =>/ api/1ek2n5gsut/shop/[login_menu_href] =>/api/1ek2n5gsut/shop/[bind_action_attr] => 0 [menu_order] => 4) [1] => Array ([menu_name] => I want to distribute [login_menu_name] => distribution center [icon] => 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] => shopping cart [login_menu_name] => [icon] => 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] => Personal Center [login_menu_name] => [icon] => 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] => classification [login_menu_name] => [icon] => 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] =>

)


  $ Row) {$ menu_order [$ key] = $ row ['menu _ order'];} // sort the data in ascending order according to menu_order // use $ menu as the last parameter and sort array_multisort ($ menu_order, SORT_ASC, $ menu) with a general key );

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

Use custom sorting

usort($menu, function($a, $b) {    return $a['menu_order'] < $b['menu_order'] ? -1 : 1;});

Just find the value of the corresponding node in the custom sorting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.