<?php$prize_arr = Array (' 0 ' = = Array (' id ' = = 1, ' title ' = ' iphone5s ', ' V ' = 5), ' 1 ' = = Array ( ' id ' = 2, ' title ' = ' Contact notebook ', ' V ' + ', ' 2 ' = = Array (' id ' = + 3, ' title ' = ' Speaker device ', ' V ' = 20), ' 3 ' = array (' id ' = = 4, ' title ' = ' 30GU ', ' V ' = ') ', ' 4 ' = = ' Array (' id ' = = 5, ' title ' = ' = ') Call $50 ', ' V ' = +, ' 5 ' = = Array (' id ' = + 6, ' title ' = ' + ' iphone6s ', ' V ' = +), ' 6 ' = = Array (' ID ') + 7, ' title ' = ' Thank you, continue to refuel Oh! ~ ', ' V ' = 10),); foreach ($prize _arr as $key = + $val) {$arr [$val [' id ']] = $val [' V '];} $prize _id = Getrand ($arr); Get the prize ID according to probability $data [' msg '] = ($prize _id = = 7)? 0:1; If 0 is not $data [' prize_title '] = $prize _arr[$prize _id-1][' title ']; Winning prizes echo Json_encode ($data); Exit Returns a JSON array to the front-end Function Getrand ($PROARR) {//calculates the winning probability $rs = ';//z winning result $proSum = Array_sum ($PROARR);//probability array total probability precision Probability array loop foreach ($proArr as $key = = $proCur) { $randNum = Mt_rand (1, $proSum); if ($randNum <= $proCur) {$rs = $key; Break } else {$proSum-= $proCur; }} unset ($PROARR); return $rs; }?>
PHP lottery probability random number