本文实例讲述了php自定义函数实现二维数组排序功能。分享给大家供大家参考,具体如下:
/**作用: 二维数组排序函数,支持多键名排序
* 返回: 排序好的数组
* 使用: array_msort(数组,需要排序的键名,排序方式);
* 例子: 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;
}
// 获取参数数量.
$ArgCount = func_num_args();
// 排序,并放置到SortRule数组
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程序设计有所帮助。
2025国考·省考课程试听报名