php二維數組排序樣本

來源:互聯網
上載者:User

例子,php二維數組排序代碼。

  1. // 說明:PHP中二維數組的排序方法
  2. // 整理: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 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 by.
  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' => '學習',
  57. 'size' => '1235',
  58. 'type' => 'jpe',
  59. 'time' => '1921-11-13',
  60. 'class' => 'dd',
  61. ),
  62. array(
  63. 'name' => '中國功夫',
  64. 'size' => '153',
  65. 'type' => 'jpe',
  66. 'time' => '2005-11-13',
  67. 'class' => 'jj',
  68. ),
  69. array(
  70. 'name' => '編程',
  71. 'size' => '35',
  72. 'type' => 'gif',
  73. 'time' => '1997-11-13',
  74. 'class' => 'dd',
  75. ),
  76. array(
  77. 'name' => '中國功夫',
  78. 'size' => '65',
  79. 'type' => 'jpe',
  80. 'time' => '1925-02-13',
  81. 'class' => 'yy',
  82. ),
  83. array(
  84. 'name' => '中國功夫',
  85. 'size' => '5',
  86. 'type' => 'icon',
  87. 'time' => '1967-12-13',
  88. 'class' => 'rr',
  89. ),
  90. );
  91. print_r($arr);
  92. //注意:按照數字方式排序時 153 比 65 小
  93. $temp = sysSortArray($arr,"name","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING");
  94. print_r($temp);
  95. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.