Home  >  Article  >  Backend Development  >  PHP generates WeChat red envelope code is simple, PHP generates red envelope code_PHP tutorial

PHP generates WeChat red envelope code is simple, PHP generates red envelope code_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:56:181000browse

PHP generates WeChat red envelope code is simple, PHP generates red envelope code

No more nonsense, all explanations are in the code, the specific code is as follows:

/**
* @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
)*/

This is the end of the code for you, I hope it will be helpful to you! More program codes for generating red envelopes in PHP can be found on the Bangkejia website. Please log in to our official website for more information, thank you!

Articles you may be interested in:

  • PHP version of WeChat public platform red envelope API
  • Using PHP to write red envelope sending program
  • PHP implements WeChat red envelope sending program
  • Jquery php randomly generates red envelope amount code sharing
  • WeChat red envelope algorithm analysis implemented by php (unofficial)
  • PHP WeChat red envelope API interface
  • php Complete list of official WeChat interfaces (WeChat Pay, WeChat Red Envelope, WeChat Shake, WeChat Store)
  • Use PHP to implement WeChat Shake peripheral red envelopes

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113694.htmlTechArticlePHP generates WeChat red envelope code is simple, PHP generates red envelope code, not much nonsense, all explanations are in the code, The specific code is as follows: /*** @param $total [The total amount of red envelopes you want to send...
Statement:
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