php - 无重复的 PHP 随机数组
问题描述
当我重新加载浏览器时,我试图做正确的事情,数组随机变化而没有重复。当我重新加载浏览器时,我看到重复的数组随机出现,请帮助我。
这是代码
<html>
<head></head>
<body>
<?php
$size = 3; $strl = "what"; $fitness1 = array("steps $strl", "dizziness $strl", "$strl symptoms ", "treatment $strl", "obesity $strl","$strl discharge");
$fitness1 = array_unique($fitness1);
$number = 1;
for ($b = 0; $b < $size ;$b++){
echo $number++ . "<table><tr><td> " . $fitness1[array_rand($fitness1)] . "<td></tr></table>";echo $number++ . "<table><tr><td> " . $fitness1[array_rand($fitness1)] . "<td></tr></table>";
for ($i = 0; $i < $size; $i++){
}
}
?>
</body>
</html>
解决方案
$i = 0;
$number = 1;
do {
shuffle($fitness1);
echo "<tr><td>" . ($number++) ."</td><td>" . array_shift($fitness1) ."</td></tr>";
echo "<tr><td>" . ($number++) ."</td><td>" . array_shift($fitness1) ."</td></tr>";
$i++;
} while ($i < $size && !empty($fitness1));
shuffle()
这个函数洗牌(随机化元素的顺序)一个数组 Blockquote
所以每次重新加载页面,顺序都会不同
https://www.php.net/manual/en/function.shuffle.php
array_shift()
将数组的第一个值移开并返回,将数组缩短一个。
https://www.php.net/manual/en/function.array-shift.php
打印值后,它将从数组中删除。所以没有重复的值
推荐阅读
- php - 为什么要将变量括在方括号中?php 7
- typechecking - 非全函数在类型级别被视为常量?
- json - 需要从单列数据框中提取 Json 数据(嵌套数组) - 表以 Null 形式出现,模式读取 -Scala
- php - 如何创建 RemoveFromCart 函数?
- python - 使用参数调用 Google Cloud 函数
- java - Java Spring将Transitional属性添加到Projector
- angular - 添加自定义 webpack 时 getProjectMetadata 不存在
- python - 我有一个关于特定模式打印的 FOR 循环列表理解的基本问题
- gremlin - 检索 Gremlin 中重复顶点的所有 ID
- javascript - 如何应用过滤器,就好像它们被推入数组一样?(其中这些数组是过滤器对象的一部分) - filter: {prop: [ ], ...}