array_rand(): 随机取值,返回值的键名array_push(): 尾部追加成员array_splice(): 删除或替换成员sort(): 排序array_flip(): 键值交换array_merge(): 数组合并
<?php// 双色球类define('RED_BALLS', range(1, 33));define('BLUE_BALLS', range(1, 16));class DoubleColorBall{//红球private static $redBalls = RED_BALLS;//篮球private static $blueBalls = BLUE_BALLS;//选中红球private static $electRedBalls = [];//选中篮球private static $electBlueBalls = [];//保存试机号private static $testNos = [];//从33个红球中选中6个红球private static function createRedBalls(){//循环拿红球for ($i = 0; $i < 6; $i++) {//1.随机取一个数,返回一个key$key = array_rand(self::$redBalls);//2.将这个球放入红球数组中array_push(self::$electRedBalls, self::$redBalls[$key]);//3.将选中的红球从33个红球中剔除array_splice(self::$redBalls, $key, 1);}//排序输出sort(self::$electRedBalls, SORT_NUMERIC);}//从16个篮球中选中1个篮球private static function createBlueBalls(){self::$electBlueBalls = array_rand(array_flip(self::$blueBalls));// print_r(self::$electBlueBalls);}//生成试机号private static function createTestNos($n=5){for ($i = 0; $i < $n; $i++) {self::$redBalls = RED_BALLS;self::$blueBalls = BLUE_BALLS;self::$electRedBalls = [];self::$electBlueBalls = null;//生成红球self::createRedBalls();//生成篮球self::createBlueBalls();//合并红蓝球成为一个中奖机号self::$testNos[] = array_merge(self::$electRedBalls, [self::$electBlueBalls]);}}//获取试机号,供外部调用public static function getTestNos($n){self::createTestNos($n);return self::$testNos;}}// DoubleColorBall::createRedBalls();// DoubleColorBall::createBlueBalls();// print_r(DoubleColorBall::getTestNos(2));// print_r(DoubleColorBall::$testNos);$result = DoubleColorBall::getTestNos(5);echo json_encode($result);
<?phprequire __DIR__ . '/demo1.php';// $n = isset($_GET['n']) ? $_GET['n'] : 5;//null 合并运算符$n = $_GET['n'] ?? 5;$allBalls = DoubleColorBall::getTestNos($n);?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>生成双色球中奖试机号</title><link rel="stylesheet" href="style.css"></head><body><div class="lottery"><h2>双色球试机号</h2><form action=""><input type="number" name="n" value="<?= $n ?>" onchange="isVip(this)"><button>生成试机号</button></form><table><tbody><?php foreach ($allBalls as $key => $balls) : ?><tr><td><?= $key + 1 ?></td><?php foreach ($balls as $ball) : ?><td><?= $ball ?></td><?php endforeach ?></tr><?php endforeach ?></tbody></table></div><script>function isVip(input){if(input.value > 10){alert('免费用户只能看10个,更多请升级VIP!');input.max = 10;input.value = 10;location.reload(true);return false;}}</script></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><button onclick="getData()">获取数据</button><script>async function getData(){const response = await fetch('demo1.php')const data = await response.json()console.log(data)}</script></body></html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号