Home > php教程 > php手册 > body text

PHP 生成微信红包代码简单,php生成红包代码

WBOY
Release: 2016-06-13 08:43:40
Original
1576 people have browsed it

PHP 生成微信红包代码简单,php生成红包代码

废话不多说了,一切解释尽在代码中,具体代码如下所示:

/**
* @param $total [你要发的红包总额]
* @param int $num [发几个]
* @return array[生成红包金额]
*/
function getRedGift($total, $num = 10)
{
$min = 0.01;
$wamp = array();
$returnData = array();
for ($i = 1; $i < $num; ++$i) {
$safe_total = ($total - ($num - $i) * $min) / ($num - $i); //红包金额的最大值
if ($safe_total < 0) break;
$money = @mt_rand($min * 100, $safe_total * 100) / 100;//随机产生一个红包金额
$total = $total - $money;//剩余红包总额
$wamp[$i] = round($money, 2);//保留两位有效数字
}
$wamp[$i] = round($total, 2);
$returnData['MoneySum'] = $wamp;
$returnData['newTotal'] = array_sum($wamp);
return $returnData;
}
//测试
$data = getRedGift(100, 10);
print_r($data);
//result:
/*
Array
(
[1] => 8.7
[2] => 10.09
[3] => 6.23
[4] => 6.87
[5] => 0.47
[6] => 3.12
[7] => 7.52
[8] => 12.21
[9] => 20.53
[10] => 24.26
)*/
Copy after login

代码到此就给大家写完了,希望对大家有所帮助!更多有关php 生成红包的程序代码尽在帮客之家网站,请登陆我们的官网了解更多资讯,谢谢!

您可能感兴趣的文章:

  • PHP版微信公众平台红包API
  • 使用PHP编写发红包程序
  • PHP实现微信发红包程序
  • jquery+php随机生成红包金额数量代码分享
  • php实现的微信红包算法分析(非官方)
  • PHP微信红包API接口
  • php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
  • 使用PHP实现微信摇一摇周边红包
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!