Php 2D array sorting example

Source: Internet
Author: User
Php 2D array sorting example

Example: php 2D array sorting code.

  1. // Description: sorting of two-dimensional arrays in PHP
  2. // Finishing: http://bbs.it-home.org
  3. /**
  4. * @ Package BugFree
  5. * @ Version $ Id: FunctionsMain. inc. php, v 1.32 11:38:37 wwccss Exp $
  6. *
  7. *
  8. * Sort an two-dimension array by some level two items use array_multisort () function.
  9. *
  10. * SysSortArray ($ Array, "Key1", "SORT_ASC", "SORT_RETULAR", "Key2 "......)
  11. * @ Author Chunsheng Wang
  12. * @ Param array $ ArrayData the array to sort.
  13. * @ Param string $ KeyName1 the first item to sort.
  14. * @ Param string $ SortOrder1 the order to sort by ("SORT_ASC" | "SORT_DESC ")
  15. * @ Param string $ SortType1 the sort type ("SORT_REGULAR" | "SORT_NUMERIC" | "SORT_STRING ")
  16. * @ Return array sorted array.
  17. */
  18. Function sysSortArray ($ ArrayData, $ KeyName1, $ SortOrder1 = "SORT_ASC", $ SortType1 = "SORT_REGULAR ")
  19. {
  20. If (! Is_array ($ ArrayData ))
  21. {
  22. Return $ ArrayData;
  23. }
  24. // Get args number.
  25. $ ArgCount = func_num_args ();
  26. // Get keys to sort by and put them to SortRule array.
  27. For ($ I = 1; $ I <$ ArgCount; $ I ++)
  28. {
  29. $ Arg = func_get_arg ($ I );
  30. If (! Eregi ("SORT", $ Arg ))
  31. {
  32. $ KeyNameList [] = $ Arg;
  33. $ SortRule [] = '$'. $ Arg;
  34. }
  35. Else
  36. {
  37. $ SortRule [] = $ Arg;
  38. }
  39. }
  40. // Get the values according to the keys and put them to array.
  41. Foreach ($ ArrayData AS $ Key => $ Info)
  42. {
  43. Foreach ($ KeyNameList AS $ KeyName)
  44. {
  45. $ {$ KeyName} [$ Key] = $ Info [$ KeyName];
  46. }
  47. }
  48. // Create the eval string and eval it.
  49. $ EvalString = 'Array _ multisort ('. join (",", $ SortRule).', $ ArrayData );';
  50. Eval ($ EvalString );
  51. Return $ ArrayData;
  52. }
  53. #################
  54. $ Arr = array (
  55. Array (
  56. 'Name' => 'learn ',
  57. 'Size' => '123 ',
  58. 'Type' => 'jpe ',
  59. 'Time' => '2017-11-13 ',
  60. 'Class' => 'DD ',
  61. ),
  62. Array (
  63. 'Name' => 'Kung Fu China ',
  64. 'Size' => '123 ',
  65. 'Type' => 'jpe ',
  66. 'Time' => '2017-11-13 ',
  67. 'Class' => 'JJ ',
  68. ),
  69. Array (
  70. 'Name' => 'programmatically ',
  71. 'Size' => '35 ',
  72. 'Type' => 'GIF ',
  73. 'Time' => '2017-11-13 ',
  74. 'Class' => 'DD ',
  75. ),
  76. Array (
  77. 'Name' => 'Kung Fu China ',
  78. 'Size' => '65 ',
  79. 'Type' => 'jpe ',
  80. 'Time' => '2017-02-13 ',
  81. 'Class' => 'yy ',
  82. ),
  83. Array (
  84. 'Name' => 'Kung Fu China ',
  85. 'Size' => '5 ',
  86. 'Type' => 'Icon ',
  87. 'Time' => '2017-12-13 ',
  88. 'Class' => 'rr ',
  89. ),
  90. );
  91. Print_r ($ arr );
  92. // Note: When sorting by number, 153 is smaller than 65.
  93. $ Temp = sysSortArray ($ arr, "name", "SORT_ASC", "type", "SORT_DESC", "size", "SORT_ASC", "SORT_STRING ");
  94. Print_r ($ temp );
  95. ?>

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.