If the value does not duplicate the case, you can use Array_flip () to Exchange keys and values, and then Krsort (), and finally Array_flip () Exchange back, you can compare size. If you want to intercept an array, you can use Array_slice ().
If there is a duplication of values, some sort algorithms are used, but PHP has a powerful function uasort () that uses a custom comparison function to sort the values in the array and keep the index associated, Usort () rebuilds the index.
Copy Code code as follows:
function cmp ($a, $b) {
if ($a ["vote_num"] = = $b ["Vote_num"]) {
return 0;
}
return ($a ["Vote_num"] > $b ["Vote_num"])? -1:1;
}
$arr = Array
(
0 => Array
(
o_id => 1861,
O_name => 2,
O_pic => ' Yun_qi_img/image.gif ',
O_detail => Everyone,
Vote_num => 1
),
1 => Array
(
o_id => 1844,
O_name => Barbie,
O_pic => ' yun_qi_img/shenxiandao.jpg ',
O_detail => is a beauty, too.
Vote_num => 2
),
2 => Array
(
o_id => 1843,
O_name => Chengcheng,
O_pic => ' Yun_qi_img/31554_4d0088da7a61ad9c8c02a530be94d98e.png ',
O_detail => Beauty Oh,
Vote_num => 3
)
);
Uasort ($arr, "CMP");
Echo ' < pre style= ' text-align:left ' > ';
Print_r ($arr);
Echo ' </PRE > ';
Return
Copy Code code as follows:
Array
(
[2] => Array
(
[O_ID] => 1843
[O_name] => Chengcheng
[O_pic] => http://g.jb51.net./upload_img/2011-06/31554_4d0088da7a61ad9c8c02a530be94d98e.png
[O_detail] => Belle OH
[Vote_num] => 3
)
[1] => Array
(
[O_ID] => 1844
[O_name] => Barbie
[O_pic] => http://upload.jb51.net/game_image/dfxxz/dfVIP.files/shenxiandao.jpg
[O_detail] => is a beautiful woman, too.
[Vote_num] => 2
)
[0] => Array
(
[O_ID] => 1861
[O_name] => 2
[O_pic] => http://g.jb51.net/image.gif
[O_detail] => Everyone
[Vote_num] => 1
)
)