Phparray_multisort () multi-group array sorting method

Source: Internet
Author: User
In php, array_multisort () can sort multiple arrays at a time, or sort multi-dimensional arrays based on one or more dimensions. if the result is successful, TRUE is returned, and FALSE is returned if the result fails.

In php, array_multisort () can sort multiple arrays at a time, or sort multi-dimensional arrays based on one or more dimensions. if the result is successful, TRUE is returned, and FALSE is returned if the result fails.

Bool array_multisort (array ar1 [, mixed arg [, mixed... [, array...])

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

Array_multisort () can be used to sort multiple arrays at a time, or to sort multi-dimensional arrays based on one or more dimensions.

The association (string) key name remains unchanged, but the number key name is re-indexed.

Example 1. the code of the multi-dimensional array sorting instance is as follows:

  1. $ Ar = array (
  2. Array ("10", 11,100,100, ""),
  3. Array (1, 2, "2", 3, 1)
  4. );
  5. Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
  6. $ Ar [1], SORT_NUMERIC, SORT_DESC );
  7. Var_dump ($ ar );
  8. ?>

In this example, after sorting, the first array will transform to "10", 100,100, 11, "a" (it was sorted as strings in ascending order ). the second will contain in 1, 3, "2", 2, 1 (sorted as numbers, in descending order ).

The instance code is as follows:

  1. Array (2 ){
  2. [0] => array (5 ){
  3. [0] => string (2) "10"
  4. [1] => int (100)
  5. [2] => int (100)
  6. [3] => int (11)
  7. [4] => string (1) ""
  8. }
  9. [1] => array (5 ){
  10. [0] => int (1)
  11. [1] => int (3)
  12. [2] => string (1) "2"
  13. [3] => int (2)
  14. [4] => int (1)
  15. }
  16. }

In this example, after sorting, the first array will contain 10,100,100, "a" (as the string ascending order), and the second array will contain 1, 3, "2 ", 1 (sort by numerical descent ).

Example 2. the Sorting multi-dimen1_array instance code is as follows:

  1. $ Ar = array (
  2. Array ("10", 11,100,100, ""),
  3. Array (1, 2, "2", 3, 1)
  4. );
  5. Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
  6. $ Ar [1], SORT_NUMERIC, SORT_DESC );
  7. Var_dump ($ ar );
  8. ?>

In this example, after sorting, the first array will become "10", 100,100, 11, "a" (sorted in ascending order as strings ). the second array will contain 1, 3, "2", 2, 1 (sorted in descending order as numbers ).

The instance code is as follows:

  1. Array (2 ){
  2. [0] => array (5 ){
  3. [0] => string (2) "10"
  4. [1] => int (100)
  5. [2] => int (100)
  6. [3] => int (11)
  7. [4] => string (1) ""
  8. }
  9. [1] => array (5 ){
  10. [0] => int (1)
  11. [1] => int (3)
  12. [2] => string (1) "2"
  13. [3] => int (2)
  14. [4] => int (1)
  15. }
  16. }

The following is a common example code for an application based on instance 3:

  1. Header ('content-Type: text/html; charset = utf-8 ');
  2. Echo'
    ';  
  3. // Original array format
  4. $ Array = array (
  5. 'Key1' => array (
  6. 'Item1' => '65 ',
  7. 'Item2' => '35 ',
  8. 'Item3' => '84 ',
  9. ),
  10. 'Key2' => array (
  11. 'Item1' => '24 ',
  12. ),
  13. 'Key3' => array (
  14. 'Item1' => '38 ',
  15. 'Item3' => '45 ',
  16. ),
  17. );
  18. // Key to be sorted
  19. // Sort by item1 in the array
  20. // You can also change to item2
  21. $ Sort = 'item1 ';
  22. Foreach ($ array as $ k => $ v)
  23. {
  24. $ NewArr [$ k] = $ v [$ sort];
  25. }
  26. // If this function is correctly executed, it will directly change the order of the original array key values
  27. // If the execution fails, bool (false) is returned)
  28. Array_multisort ($ newArr, SORT_DESC, $ array );
  29. Var_dump ($ array );
  30. // --------------------- The print effect of the sorted array starts --------------------
  31. Array (3 ){
  32. ["Key1"] =>
  33. Array (3 ){
  34. ["Item1"] =>
  35. String (2) "65"
  36. ["Item2"] =>
  37. String (2) "35"
  38. ["Item3"] =>
  39. String (2) "84"
  40. }
  41. ["Key3"] =>
  42. Array (2 ){
  43. ["Item1"] =>
  44. String (2) "38"
  45. ["Item3"] =>
  46. String (2) "45"
  47. }
  48. ["Key2"] =>
  49. Array (1 ){
  50. ["Item1"] =>
  51. String (2) "24"
  52. }
  53. }
  54. // --------------------- The print effect of the sorted array is over ---------------------

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.