本文实例讲述了PHP二维数组排序简单实现方法。分享给大家供大家参考,具体如下:
function multi_compare($a, $b){ $val_arr = array( 'gold'=>'asc', 'silver'=>'desc'//还可以增加额外的排序条件 ); foreach($val_arr as $key => $val){ if($a[$key] == $b[$key]){ continue; } return (($val == 'desc')?-1:1) * (($a[$key] < $b[$key]) ? -1 : 1); } return 0;}$arr = array( array('gold'=>1, 'silver'=>2), array('gold'=>8, 'silver'=>10), array('gold'=>8, 'silver'=>8), array('gold'=>2, 'silver'=>1),);uasort($arr, 'multi_compare');print_r($arr);
运行结果如下:
Array( [0] => Array ( [gold] => 1 [silver] => 2 ) [3] => Array ( [gold] => 2 [silver] => 1 ) [1] => Array ( [gold] => 8 [silver] => 10 ) [2] => Array ( [gold] => 8 [silver] => 8 ))