本文實例講述了php自定義函數(shù)實現(xiàn)二維數(shù)組排序功能。分享給大家供大家參考,具體如下:
/**作用: 二維數(shù)組排序函數(shù),支持多鍵名排序
* 返回: 排序好的數(shù)組
* 使用: array_msort(數(shù)組,需要排序的鍵名,排序方式);
* 例子: array_msort($cflist,"chapter_orderid","SORT_ASC");
* array_msort($arr,"name","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING");
*/
function array_msort($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR") {
if(!is_array($ArrayData)) {
return $ArrayData;
}
// 獲取參數(shù)數(shù)量.
$ArgCount = func_num_args();
// 排序,并放置到SortRule數(shù)組
for($i = 1;$i < $ArgCount;$i ++) {
$Arg = func_get_arg($i);
if(!eregi("SORT",$Arg)) {
$KeyNameList[] = $Arg;
$SortRule[] = '$'.$Arg;
}
else {
$SortRule[] = $Arg;
}
}
// Get the values according to the keys and put them to array.
foreach($ArrayData AS $Key => $Info) {
foreach($KeyNameList AS $KeyName) {
${$KeyName}[$Key] = $Info[$KeyName];
}
}
// Create the eval string and eval it.
$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
eval($EvalString);
return $ArrayData;
}
希望本文所述對大家PHP程序設(shè)計有所幫助。