PHP抽奖代码

1,'prize'=>'iphone7','v'=>1),
            array('id'=>2,'prize'=>'平板电脑','v'=>10),
            array('id'=>3,'prize'=>'100元红包','v'=>15),
            array('id'=>4,'prize'=>'50元红包','v'=>24),
            array('id'=>5,'prize'=>'很遗憾没有中奖','v'=>50)
        );
    //抽奖算法
    function get_rand($arr)
    {
        $result = '';

        $sum = array_sum($arr);

        foreach ($arr as $key => $value) 
        {
            $r = mt_rand(1,$sum);
            if($r <= $value)
            {
                $result = $key;
                break;
            }else
            {
                $sum -= $value;
            }       
        }
        unset($arr);
        return $result;
    }
    //抽奖
    foreach ($arr_prize as $key => $value) 
    {
         $arr[$value['id']] = $value['v'];
    }
    $id = get_rand($arr);
    $res['yes'] = $arr_prize[$id-1]['prize'];
    unset($arr_prize[$id-1]);//把中奖项删除
    shuffle($arr_prize); //打乱数组顺序 
    for($i=0;$i

你可能感兴趣的:(PHP抽奖代码)