PHP Array_multisort Examples of multidimensional array sorting

Source: Internet
Author: User
    1. $arr 1 = array (1,9,5);
    2. $arr 2 = array (6,2,4);
    3. Array_multisort ($arr 1, $arr 2);
    4. Print_r ($arr 1); The order to get is 1,5,9
    5. Print_r ($arr 2); The order to get is 6,4,2
Copy Code

I estimate that the values of the two arrays correspond from start to finish: 1 corresponds to 2,5 corresponding to 6,9 4. Add one more array to see what happens:

    1. $arr 1 = array (1,9,5);
    2. $arr 2 = array (6,2,4);
    3. $arr 3 = array (3,7,8);
    4. Array_multisort ($arr 1, $arr 2, $arr 3);
Copy Code

To view the results, 1 corresponds to 6 corresponding to 3, and so are the other items. This correspondence is what is called in the manual "preserve the original Key name association". Alternatively, you can think of each array as a column of the database table. and the corresponding 1,6,3 is a data row, 9,2,7 for another data row ... Array_multisort is sorted by the first array (pictured as a column), sorted by the second array (column) if the value of the first array (column) is the same. Test:

    1. $arr 1 = array (1,9,5,9);
    2. $arr 2 = array (6,2,4,1);
    3. $arr 3 = array (3,7,8,0);
    4. Array_multisort ($arr 1, $arr 2, $arr 3);
Copy Code

You can imagine the result of $ARR3 here is (3,8,0,7).

Second, the parameters of Array_multisort.

The simplest case is that, as shown above, with 1 or n arrays as parameters, it is important to note that the number of items in each array is the same, otherwise warning will cause the sort to fail. Like this array_multisort ($arr 1, $arr 2, $arr 3); The default is that all arrays are in ascending order, if you want to $arr2 descending, and as a string to compare, it should be written: Array_multisort ($arr 1, $arr 2, Sort_desc, sort_string, $arr 3); Each array can be followed by a sort order flag or a sort type flag, or both flags appear at the same time. However, each sort flag can only appear after each array. Sort order Flag: sort_asc-Sort by ascending order (default) Sort_desc-Sort by descending order

Sort type flag: sort_regular-Compare items by usual method (default) Sort_numeric-Compare items by numeric value sort_string-Compare items by string

Three, array_multisort the actual function usually some multidimensional array need to sort:

    1. $guys = Array
    2. (
    3. [0] = = Array
    4. (
    5. [Name] and Jake
    6. [Score] = 80
    7. [Grade] = A
    8. )
    9. [1] = = Array
    10. (
    11. [Name] = Jin
    12. [Score] = 70
    13. [Grade] = A
    14. )
    15. [2] = = Array
    16. (
    17. [Name] = John
    18. [Score] = 80
    19. [Grade] = A
    20. )
    21. [3] = = Array
    22. (
    23. [Name] = Ben
    24. [Score] = 20
    25. [Grade] = B
    26. )
    27. )
Copy Code

For the use of PHP array function array_multisort (), you can also read the following articles: PHP array function array_map, Array_multisort multi-dimensional array ordering instance PHP array sorting function Array_ The difference between Multisort and Uasort php array function array_multisort () Usage in PHP array_multisort () Use instances for example, if you want to rank in reverse order, if the results are the same, sort by the ascending name. Then we need to get two more arrays according to the order of $guys: $scores = Array (80,70,80,20), $names = Array (' Jake ', ' Jin ', ' John ', ' Ben ') and Array_multisort ($scores, Sort_desc, $names, $guys); Can you be more flexible, do you want to sort each other to get some extra arrays? In fact, in the qeephp of the Helper_array class has been encapsulated very well, the following is its two methods, we use a few changes can be:

  1. /**
  2. * Sorts the array according to the specified key
  3. *
  4. Usage
  5. * @code PHP
  6. * $rows = Array (
  7. * Array (' id ' = = 1, ' value ' = ' 1-1 ', ' parent ' = 1),
  8. * Array (' id ' = = 2, ' value ' = ' 2-1 ', ' parent ' = 1),
  9. * Array (' id ' = = 3, ' value ' = ' 3-1 ', ' parent ' = 1),
  10. * Array (' id ' = = 4, ' value ' = ' 4-1 ', ' parent ' = 2),
  11. * Array (' id ' = = 5, ' value ' = ' 5-1 ', ' parent ' = 2),
  12. * Array (' id ' = = 6, ' value ' = ' 6-1 ', ' parent ' = 3),
  13. * );
  14. *
  15. * $rows = Helper_array::sortbycol ($rows, ' id ', SORT_DESC);
  16. * Dump ($rows);
  17. *//Output Result:
  18. *//Array (
  19. *//Array (' id ' = = 6, ' value ' = ' 6-1 ', ' parent ' = + 3),
  20. *//Array (' id ' = = 5, ' value ' = ' 5-1 ', ' parent ' = + 2),
  21. *//Array (' id ' = = 4, ' value ' = ' 4-1 ', ' parent ' = + 2),
  22. *//Array (' id ' = = 3, ' value ' = ' 3-1 ', ' parent ' = + 1),
  23. *//Array (' id ' = = 2, ' value ' = ' 2-1 ', ' parent ' = + 1),
  24. *//Array (' id ' = = 1, ' value ' = ' 1-1 ', ' parent ' = + 1),
  25. * // )
  26. * @endcode
  27. *
  28. * @param array $array to sort
  29. * @param string $keyname sort the key
  30. * @param int $dir sort Direction
  31. *
  32. * @return arrays sorted by array
  33. */
  34. static function Sortbycol ($array, $keyname, $dir = SORT_ASC)
  35. {
  36. Return Self::sortbymulticols ($array, array ($keyname = $dir));
  37. } /**
  38. * Sort a two-dimensional array in multiple columns, similar to an ORDER by in an SQL statement
  39. *
  40. Usage
  41. * @code PHP
  42. * $rows = Helper_array::sortbymulticols ($rows, Array (
  43. * ' Parent ' = SORT_ASC,
  44. * ' name ' = = Sort_desc,
  45. * ));
  46. * @endcode
  47. *
  48. * @param array $rowset to sort
  49. * @param array $args sorted keys
  50. *
  51. * @return arrays sorted by array
  52. */
  53. static function Sortbymulticols ($rowset, $args)
  54. {
  55. $sortArray = Array ();
  56. $sortRule = ";
  57. foreach ($args as $sortField = $sortDir)
  58. {
  59. foreach ($rowset as $offset = $row)
  60. {
  61. $sortArray [$sortField] [$offset] = $row [$sortField];
  62. }
  63. $sortRule. = ' $sortArray [\ '. $sortField. '\'], ' . $sortDir. ', ';
  64. }
  65. if (Empty ($sortArray) | | empty ($sortRule)) {return $rowset;}
  66. Eval (' Array_multisort ('. $sortRule. ' $rowset);
  67. return $rowset;
  68. }
Copy Code
  • 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.