如果值没有重复的情况,可以先用array_flip()来交换键和值,然后krsort(),最后再array_flip()交换回来,就可以比较大小了。如果要截取数组,可用array_slice()。
如果有值重复的情况,就要用到一些排序算法了,不过php有很强大的函数uasort(),使用自定义的比较函数对数组中的值进行排序并保持索引关联,usort()则会重建索引。
<BR>function cmp($a, $b){ <BR>if ($a["vote_num"] == $b["vote_num"]) { <BR>return 0; <BR>} <BR>return ($a["vote_num"] > $b["vote_num"]) ? -1 : 1; <BR>} <br><br>$arr = Array <BR>( <BR>0 => Array <BR>( <BR>o_id => 1861, <BR>o_name => 2, <BR>o_pic => 'http://g.jb51.net/image.gif' , <BR>o_detail => 人人, <BR>vote_num => 1 <BR>), <br><br>1 => Array <BR>( <BR>o_id => 1844, <BR>o_name => 芭比, <BR>o_pic => 'http://upload.jb51.net/game_image/dfxxz/dfVIP.files/shenxiandao.jpg', <BR>o_detail => 也是美女呢, <BR>vote_num => 2 <BR>), <br><br>2 => Array <BR>( <BR>o_id => 1843, <BR>o_name => 程程, <BR>o_pic => 'http://g.jb51.net./upload_img/2011-06/31554_4d0088da7a61ad9c8c02a530be94d98e.png', <BR>o_detail => 美女哦,<b>/本文来源gao@!dai!ma.com搞$$代^@码5网@</b><strong>搞代gaodaima码</strong> <BR>vote_num => 3 <BR>) <BR>); <BR>uasort($arr, ”cmp“); <BR>echo ‘<pre style="text-align:left">'; <BR>print_r ($arr); <BR>echo ‘
‘;
返回
<BR>Array <BR>( <BR>[2] => Array <BR>( <BR>[o_id] => 1843 <BR>[o_name] => 程程 <BR>[o_pic] => http://g.jb51.net./upload_img/2011-06/31554_4d0088da7a61ad9c8c02a530be94d98e.png <BR>[o_detail] => 美女哦 <BR>[vote_num] => 3 <BR>) <BR>[1] => Array <BR>( <BR>[o_id] => 1844 <BR>[o_name] => 芭比 <BR>[o_pic] => http://upload.jb51.net/game_image/dfxxz/dfVIP.files/shenxiandao.jpg <BR>[o_detail] => 也是美女呢 <BR>[vote_num] => 2 <BR>) <BR>[0] => Array <BR>( <BR>[o_id] => 1861 <BR>[o_name] => 2 <BR>[o_pic] => http://g.jb51.net/image.gif <BR>[o_detail] => 人人 <BR>[vote_num] => 1 <BR>) <BR>) <BR>