1. 排序方法
//排序方法
function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR"){
if(!is_array($ArrayData)){
return $ArrayData;
}
// Get args number.
$ArgCount = func_num_args();
// Get keys to sort by and put them to SortRule array.
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;
}
2. 页面调用
$array1 = Array( 'userid' => '102',
'name' => 'test1',
'auth' => '0',
'stocktodaygain' => '4000',
'sp_todayPer' => '0.0034',
'futuretodaygain' => '10216.85',
'fp_todayPer' => '0.059',
'marketmoney' => '1056710.9',
'todayPer' => '0.012' );
$array2 = Array( 'userid' => '103',
'name' => 'test3',
'auth' => '0',
'stocktodaygain' => '3000',
'sp_todayPer' => '0.0056',
'futuretodaygain' => '10216.85',
'fp_todayPer' => '0.061',
'marketmoney' => '1056710.9',
'todayPer' => '0.011' );
$array = array();
//把两个数组的内容加入到一个二维数组中
array_push($array,$array1);
array_push($array,$array2);
//调用排序方法 例子以sp_todayPer进行正序排例, 想要倒序排列把第三个参数值改为SORT_DESC
$temp = sysSortArray($array,"sp_todayPer","SORT_ASC");
3. 输出结果为:
Array( [0] => Array ( [userid] => 102 [name] => test1 [auth] => 0 [stocktodaygain] => 4000 [sp_todayPer] => 0.0034 [futuretodaygain] => 10216.85 [fp_todayPer] => 0.059 [marketmoney] => 1056710.9 [todayPer] => 0.012 ) [1] => Array ( [userid] => 103 [name] => test3 [auth] => 0 [stocktodaygain] => 3000 [sp_todayPer] => 0.0056 [futuretodaygain] => 10216.85 [fp_todayPer] => 0.061 [marketmoney] => 1056710.9 [todayPer] => 0.011 ))