PHP中二维数组的排序方法

来源:岁月联盟 编辑:lion 时间:2010-08-11

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

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