程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> PHP排序之二維數組的按照字母排序實現代碼

PHP排序之二維數組的按照字母排序實現代碼

編輯:PHP綜合
復制代碼 代碼如下:
<?php
/**
* Sort an two-dimension array by some level two items use array_multisort() function.
*
* sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)
* @author Chunsheng Wang <[email protected]>
* @param array $ArrayData the array to sort.
* @param string $KeyName1 the first item to sort by.
* @param string $SortOrder1 the order to sort by("SORT_ASC"|"SORT_DESC")
* @param string $SortType1 the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
* @return array sorted array.
*/
function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
{
if(!is_array($ArrayData))
{
return $ArrayData;
}
$ArgCount = func_num_args();
for($I = 1;$I < $ArgCount;$I ++)
{
$Arg = func_get_arg($I);
if(!eregi("SORT",$Arg))
{
$KeyNameList[] = $Arg;
$SortRule[] = '$'.$Arg;
}
else
{
$SortRule[] = $Arg;
}
}
foreach($ArrayData AS $Key => $Info)
{
foreach($KeyNameList AS $KeyName)
{
${$KeyName}[$Key] = $Info[$KeyName];
}
}
$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
eval ($EvalString);
return $ArrayData;
}
//################# 示例 #################
$arr = array(
array(
'name' => '學習',
'size' => '1235',
'type' => 'jpe',
'time' => '1921-11-13',
'class' => 'D',
),
array(
'name' => '中國功夫',
'size' => '153',
'type' => 'jpe',
'time' => '2005-11-13',
'class' => 'J',
),
array(
'name' => '編程',
'size' => '35',
'type' => 'gif',
'time' => '1997-11-13',
'class' => 'A',
),
array(
'name' => '中國功夫',
'size' => '65',
'type' => 'jpe',
'time' => '1925-02-13',
'class' => 'D',
),
array(
'name' => '中國功夫',
'size' => '5',
'type' => 'icon',
'time' => '1967-12-13',
'class' => 'C',
),
);
print_r($arr);
//注意:按照數字方式排序時 153 比 65 小
$temp = sysSortArray($arr,"class","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING");
echo "<pre>";
print_r($temp);
?>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved