1. public function choujiang()
    2. {
    3. // 奖品数组
    4. $proArr = [
    5. ['id' => 1, 'name' => '特等奖', 'v' => 1],
    6. ['id' => 2, 'name' => '一等奖', 'v' => 4],
    7. ['id' => 3, 'name' => '二等奖', 'v' => 5],
    8. ['id' => 4, 'name' => '三等奖', 'v' => 10],
    9. ['id' => 5, 'name' => '四等奖', 'v' => 30],
    10. ['id' => 6, 'name' => '谢谢惠顾', 'v' => 50],
    11. ];
    12. dump($this->get_rand($proArr));
    13. }
    14. private function get_rand($proArr)
    15. {
    16. $result = array();
    17. foreach ($proArr as $key => $val) {
    18. $arr[$key] = $val['v'];
    19. }
    20. dump("抽奖概率");
    21. dump($arr);
    22. // 概率数组的总概率
    23. $proSum = array_sum($arr);
    24. asort($arr);
    25. // 概率数组循环
    26. foreach ($arr as $k => $v) {
    27. $randNum = mt_rand(1, $proSum);
    28. if ($randNum <= $v) {
    29. $result = $proArr[$k];
    30. break;
    31. } else {
    32. $proSum -= $v;
    33. }
    34. }
    35. return $result;
    36. }