几种排名算法
作者:互联网
自上次写完随笔以来,2个多月没写啦,这次总结下几种工作中常用的排名。其实比较简单,我们争取用最少的代码实现功能。
准备数组:
$info = [
["id" => "a", "vote_num" => 7],
["id" => "b", "vote_num" => 3],
["id" => "c", "vote_num" => 3],
["id" => "d", "vote_num" => 7],
["id" => "e", "vote_num" => 2],
["id" => "f", "vote_num" => 5],
];
第一种:
常规排序:(根据数组的键值拿到排序)
array_multisort(array_column($info,'vote_num'),SORT_DESC,$info);
foreach ($info as $k=>&$item){
$item['rank'] = $k+1;
}
echo '<pre>';
print_r($info);
标签:info,几种,item,算法,num,vote,排名,array,id 来源: https://www.cnblogs.com/wujf-myblog/p/13913550.html