PHP two-dimensional array ordering example

Source: Internet
Author: User

Example, php two-dimensional array sorting code.

  1. Description: A two-dimensional array ordering method in PHP
  2. Finishing: http://bbs.it-home.org
  3. /**
  4. * @package Bugfree
  5. * @version $Id: functionsmain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss EXP $
  6. *
  7. *
  8. * Sort an Two-dimension array by some level the 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 $KeyName 1 The first item to sort by.
  14. * @param string $SortOrder 1 The order to sort by ("Sort_asc" | " Sort_desc ")
  15. * @param string $SortType 1 the Sort type ("Sort_regular" | " Sort_numeric "|" Sort_string ")
  16. * @return Array sorted array.
  17. */
  18. function Syssortarray ($ArrayData, $KeyName 1, $SortOrder 1 = "Sort_asc", $SortType 1 = "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. ################# Example #################
  54. $arr = Array (
  55. Array
  56. ' Name ' = ' Learn ',
  57. ' Size ' = ' 1235 ',
  58. ' Type ' = ' jpe ',
  59. ' Time ' = ' 1921-11-13 ',
  60. ' Class ' = ' DD ',
  61. ),
  62. Array
  63. ' Name ' = ' Chinese Kung fu ',
  64. ' Size ' = ' 153 ',
  65. ' Type ' = ' jpe ',
  66. ' Time ' = ' 2005-11-13 ',
  67. ' Class ' = ' JJ ',
  68. ),
  69. Array
  70. ' Name ' = ' programming ',
  71. ' Size ' = ' 35 ',
  72. ' Type ' = ' gif ',
  73. ' Time ' = ' 1997-11-13 ',
  74. ' Class ' = ' DD ',
  75. ),
  76. Array
  77. ' Name ' = ' Chinese Kung fu ',
  78. ' Size ' = ' 65 ',
  79. ' Type ' = ' jpe ',
  80. ' Time ' = ' 1925-02-13 ',
  81. ' Class ' = ' yy ',
  82. ),
  83. Array
  84. ' Name ' = ' Chinese Kung fu ',
  85. ' Size ' = ' 5 ',
  86. ' Type ' = ' icon ',
  87. ' Time ' = ' 1967-12-13 ',
  88. ' Class ' = ' rr ',
  89. ),
  90. );
  91. Print_r ($arr);
  92. Note: When sorting by number 153:65 Small
  93. $temp = Syssortarray ($arr, "name", "Sort_asc", "type", "Sort_desc", "size", "Sort_asc", "sort_string");
  94. Print_r ($temp);
  95. ?>
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.