當我重新加載瀏覽器時,我試圖做正確的事情,數組隨機變化而沒有重復。當我重新加載瀏覽器時,我看到重復的數組隨機出現,請幫助我。這是代碼<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>
1 回答

叮當貓咪
TA貢獻1776條經驗 獲得超12個贊
$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
打印值后,它將從數組中刪除。所以沒有重復的值
- 1 回答
- 0 關注
- 78 瀏覽
添加回答
舉報
0/150
提交
取消