PHP中二维数组的排序方法

作者: BugFree     来源: BugFree     时间: 2006-10-25 16:44:45
摘要: 本文介绍的是从 BugFree 摘录来的二维数组排序函数,可以实现类似 MySQL 的 ORDER BY 效果,当数组不是从数据库取得时会有特殊应用。
归类: PHP/MySQL,

关键词: 数组排序, array_multisort, array, sort,

本文介绍的是从 BugFree 摘录来的二维数组排序函数,可以实现类似 MySQL 的 ORDER BY 效果,当数组不是从数据库取得时会有特殊应用。 


PHP:
  1.  
  2. <?php
  3. // 说明:PHP中二维数组的排序方法
  4. // 整理:http://www.CodeBit.cn
  5.  
  6. /**
  7. * @package BugFree
  8. * @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
  9. *
  10. *
  11. * Sort an two-dimension array by some level two items use array_multisort() function.
  12. *
  13. * sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)
  14. * @author Chunsheng Wang <wwccss@263.net>
  15. * @param array $ArrayData the array to sort.
  16. * @param string $KeyName1 the first item to sort by.
  17. * @param string $SortOrder1 the order to sort by("SORT_ASC"|"SORT_DESC")
  18. * @param string $SortType1 the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
  19. * @return array sorted array.
  20. */
  21. function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
  22. {
  23. if(!is_array($ArrayData))
  24. {
  25. return $ArrayData;
  26. }
  27.  
  28. // Get args number.
  29. $ArgCount = func_num_args();
  30.  
  31. // Get keys to sort by and put them to SortRule array.
  32. for($I = 1;$I < $ArgCount;$I ++)
  33. {
  34. $Arg = func_get_arg($I);
  35. if(!eregi("SORT",$Arg))
  36. {
  37. $KeyNameList[] = $Arg;
  38. $SortRule[] = '$'.$Arg;
  39. }
  40. else
  41. {
  42. $SortRule[] = $Arg;
  43. }
  44. }
  45.  
  46. // Get the values according to the keys and put them to array.
  47. foreach($ArrayData AS $Key => $Info)
  48. {
  49. foreach($KeyNameList AS $KeyName)
  50. {
  51. ${$KeyName}[$Key] = $Info[$KeyName];
  52. }
  53. }
  54.  
  55. // Create the eval string and eval it.
  56. $EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
  57. eval ($EvalString);
  58. return $ArrayData;
  59. }
  60.  
  61.  
  62. //################# 示例 #################
  63. $arr = array(
  64. array(
  65. 'name' => '学习',
  66. 'size' => '1235',
  67. 'type' => 'jpe',
  68. 'time' => '1921-11-13',
  69. 'class' => 'dd',
  70. ),
  71. array(
  72. 'name' => '中国功夫',
  73. 'size' => '153',
  74. 'type' => 'jpe',
  75. 'time' => '2005-11-13',
  76. 'class' => 'jj',
  77. ),
  78. array(
  79. 'name' => '编程',
  80. 'size' => '35',
  81. 'type' => 'gif',
  82. 'time' => '1997-11-13',
  83. 'class' => 'dd',
  84. ),
  85. array(
  86. 'name' => '中国功夫',
  87. 'size' => '65',
  88. 'type' => 'jpe',
  89. 'time' => '1925-02-13',
  90. 'class' => 'yy',
  91. ),
  92. array(
  93. 'name' => '中国功夫',
  94. 'size' => '5',
  95. 'type' => 'icon',
  96. 'time' => '1967-12-13',
  97. 'class' => 'rr',
  98. ),
  99. );
  100.  
  101. print_r($arr);
  102.  
  103. //注意:按照数字方式排序时 153 比 65 小
  104. $temp = sysSortArray($arr,"name","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING");
  105.  
  106. print_r($temp);
  107.  
  108. ?>
  109.  



推荐链接:(联系 QQ :326801485)