自上次写完随笔以来,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);