Php multi-dimensional array sorting example

Source: Internet
Author: User
Php multi-dimensional array sorting example

  1. $ Array = array (

  2. '0' => array ('3', 'one '),
  3. '1' => array ('20140901', 'two '),
  4. '2' => array ('12', 'Three '),
  5. '3' => array ('13', 'Four '),
  6. '4' => array ('1', 'five '),
  7. '5' => array ('3', 'six '),
  8. );

  9. /* Method 1: The uasort () function is used instead of the usort () function. The difference is that uasort () retains the original index and usort () reconstructs the index. */

  10. Function cmp ($ a, $ B ){
  11. If ($ a [0] = $ B [0]) {
  12. Return 0;
  13. }
  14. Return ($ a [0] <$ B [0])? -1: 1;
  15. }

  16. Uasort ($ array, "cmp ");

  17. Print_r ($ array );

  18. Echo"
    **********************
    ";

  19. /* Method 2: Use array_multisort */

  20. Foreach ($ array as $ key => $ value ){
  21. $ First [$ key] = $ value [0];
  22. $ Second [$ key] = $ value [1];
  23. }
  24. Array_multisort ($ first, SORT_NUMERIC, SORT_ASC, $ second, SORT_STRING, SORT_ASC, $ array );
  25. Print_r ($ array );
  26. /*
  27. Sort order mark:
  28. SORT_ASC-sort by ascending order
  29. SORT_DESC-sort by descent

  30. Sorting type flag:

  31. SORT_REGULAR-compare projects by common methods
  32. SORT_NUMERIC-compare projects by numerical values
  33. SORT_STRING-compare items by string
  34. */

>>> For more information, see The php array sorting method <

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.