public function actionRank(){
// 生成随机字符串
function get_random($len,$chars)
{
$hash = "";
$max = strlen($chars) - 1;
for ($i=0; $i < $len; $i++) {
$hash .= $chars[mt_rand(0,$max)];
}
return $hash;
}
$dd = Yii::$app->redis;
$cachkey = "rank_";
$check = $dd->exists($cachkey);
if($check){
$dd->del($cachkey);
}
for($i=0;$i<5;$i++){
$num = rand(0,100);
$str = get_random(6,'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ');
$dd->ZADD($cachkey,$num,'name='.$str);
}
//由大到小排序 ZREVRANGE salary 0 -1 WITHSCORES # 递减排列
$dataOne = $dd->ZREVRANGE($cachkey,0,-1,WITHSCORES);
// 由小到大排序 ZRANGE salary 0 -1 WITHSCORES # 递增排列
$dataTow = $dd->ZRANGE($cachkey,0,-1,WITHSCORES);
// print_r($dataOne);
print_r($dataOne);
$res1 = array();$i=0;
foreach ($dataOne as $k=>$v){
if(!is_numeric($v)){
//$ranks = $dd->ZRANK($cachkey,$v); //排名
//$res['ranks']=$ranks;
$res['name']=substr($v,5,-1);
}else{
$res['score']=$v;
}
array_push($res1,$res);
$i++;
}
$res2 = array();$ii=0;
foreach ($res1 as $k1=>$v1){
if(count($v1)==2 && $k1%2==1){
$res2[$ii]['name'] = $v1['name'];
$res2[$ii]['ranks'] = $ii+1;
$res2[$ii]['score'] = $v1['score'];
$ii++;
}
}
print_r($res2);
}