/** * @param $total [total amount of red packets you want to send] * @param int $num [send several] * @return array[generate red envelope amount] */function getredgift ($total, $num = ten) {
$min = 0.01; $wamp = Array (); $returnData = Array (); for ($i = 1; $i < $num, + + $i) { $safe _total = ($total-($num-$i) * $min)/($num-$i); _total < 0) break; $money = @mt_rand ($min *, $safe _total * 100)/100;//randomly generates a red envelope amount $total = $total-$money;//The total amount of the remaining red packets $wamp [$i] = Round ($money, 2);//reserved two valid digits } $wamp [$i] = Round ($total, 2); $returnData [' moneysum '] = $wamp; $returnData [' newtotal '] = array_sum ($wamp); return $returnData;} Test $data = Getredgift (1),;p Rint_r ($data),//result:/*array ([+] = 8.7 [2] = 10.09 [3] = = 6.23 [4] = = 6. [5] = = 0.47 [6] = 3.12 [7] = 7.52 [8] = = 12.21 [9] = 20.53 [ten] = 24.26) */