如何对以下数组进行排序
Array
(
[17] => Array
(
[network_id] => 17
[network_name] => M4N
[total_transactions] => 4
[commission] => 6.08
[ordervalue] => 101
[pending] => 0
[approved] => 4
)
[18] => Array
(
[network_id] => 18
[network_name] => Zanox
[total_transactions] => 32
[commission] => 33.7
[ordervalue] => 1010.45
[pending] => 0
[approved] => 32
)
)
我想按照排序选项
对上面的数组进行排序我试图使用array_multisort
对其进行排序,但到目前为止还没有成功
答案 0 :(得分:1)
尝试
function sort_custom_array($arr,$key,$sort='asc'){
foreach($arr as &$r){
$network_id[] = $r['network_id'];
$network_name[] = $r['network_name'];
$total_transactions[] = $r['total_transactions'];
$commission[] = $r['commission'];
$ordervalue[] = $r['ordervalue'];
$pending[] = $r['pending'];
$approved[] = $r['approved'];
}
$sort = ($sort=='asc')? constant('SORT_ASC') : constant('SORT_DESC');
switch($key){
case 'network_id':
array_multisort($network_id, $sort, $arr);
break;
case 'network_name':
array_multisort($network_name, SORT_NATURAL , $arr);
if($sort==3){
$arr = array_reverse($arr, true);
}
break;
case 'total_transactions':
array_multisort($total_transactions, $sort, $arr);
break;
case 'commission':
array_multisort($commission, $sort, $arr);
break;
case 'ordervalue':
array_multisort($ordervalue, $sort, $arr);
break;
case 'pending':
array_multisort($pending, $sort, $arr);
break;
case 'approved':
array_multisort($approved, $sort, $arr);
break;
}
return $arr;
}
现在您可以对此数组中的字段进行排序,如下所示:
$arr1 = sort_custom_array($arr,'ordervalue','desc');
echo "<pre>";
print_r($arr1);
请参阅演示here
答案 1 :(得分:0)
你可以使用usort();按用户定义的比较排序。