亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無重復的 PHP 隨機數組

無重復的 PHP 隨機數組

PHP
元芳怎么了 2022-10-09 17:20:50
當我重新加載瀏覽器時,我試圖做正確的事情,數組隨機變化而沒有重復。當我重新加載瀏覽器時,我看到重復的數組隨機出現,請幫助我。這是代碼<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

打印值后,它將從數組中刪除。所以沒有重復的值


查看完整回答
反對 回復 2022-10-09
  • 1 回答
  • 0 關注
  • 78 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號