抽奖 foreach ($prize_arr as $k=$v) { $per = $v['num']/$total;//var_dump($v['num']); var_dump($total); print_r($per); $per = round($per*100,2);/*百分比*/ //var_dump($per); $arr[$v['id']] = $per; //$arr[$v['id']] = $v['v'];}//print_r($pr
foreach ($prize_arr as $k=>$v) { $per = $v['num']/$total;//var_dump($v['num']); var_dump($total); print_r($per); $per = round($per*100,2);/*百分比*/ //var_dump($per); $arr[$v['id']] = $per; //$arr[$v['id']] = $v['v']; } //print_r($prize_arr); //print_r($arr); //exit; $prize_id = getRand($arr); //根据概率获取奖项id foreach($prize_arr as $k=>$v){ //获取前端奖项位置 if($v['id'] == $prize_id){ $prize_site = $k; break; } } $res = $prize_arr[$prize_id - 1]; //中奖项 //var_dump($res); $data['prize_name'] = $res['prize']; $data['prize_site'] = $prize_site;//前端奖项从-1开始 $data['prize_id'] = $prize_id; echo json_encode($data); function getRand($proArr) { $data = ''; $proSum = array_sum($proArr); //概率数组的总概率精度 foreach ($proArr as $k => $v) { //概率数组循环 $randNum = mt_rand(1, $proSum); if ($randNum <= $v) { $data = $k; break; } else { $proSum -= $v; } } unset($proArr); return $data; }lottery.zip lottery.zip