Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1578686
  • 博文数量: 50
  • 博客积分: 9971
  • 博客等级: 中将
  • 技术积分: 2615
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-03 16:03
文章分类

全部博文(50)

文章存档

2011年(2)

2010年(2)

2009年(41)

2008年(5)

我的朋友

分类:

2009-04-29 17:01:44

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


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