1 //二维数组排序
 2 $good = array();
 3 
 4 for($i = 0; $i<7 ; $i++ ){
 5     
 6     $good[$i]['price']  = rand(1,10000);
 7     $good[$i]['hot']    = rand(1,100);
 8     $good[$i]['follow'] = rand(1,1000);    
 9 
10 }
11 
12 echo '<pre>';
13 
14 print_r($good);
15 
16 echo '</pre>';
17 
18 $hot=array();
19 $follow=array();
20 
21 foreach($good as $k=>$v){
22     $hot[$k] = $v['hot'];
23     $follow[$k] = $v['follow'];
24 }
25 
26 //二维数组排序 先按hot字段降序 ,再按follow降序
27 array_multisort($hot,SORT_DESC,$follow,SORT_DESC,$good);
28 
29 echo '<pre>';
30 
31 print_r($good);
32 
33 echo '</pre>';

 

//方法二
function xx($a, $b)
{
    if ($a['price'] == $b['price']) {
        return ($a['hot'] < $b['hot']) ? 1 : -1; ;
    }
    return ($a['price'] < $b['price']) ? 1 : -1;
}

$a = array(
0=>array('price'=>123,'hot'=>34543),
1=>array('price'=>434,'hot'=>234),
2=>array('price'=>42,'hot'=>2232),
3=>array('price'=>42,'hot'=>235432),
4=>array('price'=>33443,'hot'=>12),
4=>array('price'=>434,'hot'=>1211),
);

usort($a, 'xx');

echo '<pre>';

print_r($a);

echo '</pre>';

 

posted on 2013-05-07 17:20  睡着的糖葫芦  阅读(284)  评论(0编辑  收藏  举报