【发布时间】:2017-10-17 10:40:13
【问题描述】:
我有以下数组
$postarray[] = array( 'total' => '6.5', 'points' => '300' );
$postarray[] = array( 'total' => '11.4', 'points' => '320' );
$postarray[] = array( 'total' => '6.5', 'points' => '340' );
$postarray[] = array( 'total' => '2.6', 'points' => '300' );
$postarray[] = array( 'total' => '12.8', 'points' => '320' );
我想将它排序成以下内容。请注意,如果 points 相等,则它会比较 total 并按升序对其进行排序。
$postarray[] = array( 'total' => '6.5', 'points' => '340' );
$postarray[] = array( 'total' => '12.8', 'points' => '320' );
$postarray[] = array( 'total' => '11.4', 'points' => '320' );
$postarray[] = array( 'total' => '6.5', 'points' => '300' );
$postarray[] = array( 'total' => '2.6', 'points' => '300' );
到目前为止,我已经使用usort 根据points 进行排序,但我也不确定如何对total 字段进行排序。
function sortByOrder($a, $b) {
if ($b['points'] > $a['points']) {
return $b['points'] - $a['points'];
} elseif ($b['points'] == $a['points']) {
return 0;
}
}
usort($postarray, 'sortByOrder');
【问题讨论】: