简体   繁体   English

PHP随机数组没有重复

[英]PHP Random Array with no duplicates

I'm trying to get something right when I reload the browser the array changes randomly with no duplicate.当我重新加载浏览器时,我试图做一些正确的事情,数组随机更改而没有重复。 When I reload the browser I saw duplicate array coming randomly, please help me.当我重新加载浏览器时,我看到重复的数组随机出现,请帮助我。

Here is the code这是代码

<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() This function shuffles (randomizes the order of the elements in) an array Blockquote shuffle()该函数对数组 Blockquote 进行随机排列(随机排列元素的顺序)

So each time you reload the page, the order will be different所以每次重新加载页面时,顺序都会不同

https://www.php.net/manual/en/function.shuffle.php https://www.php.net/manual/en/function.shuffle.php

array_shift() shifts the first value of the array off and returns it, shortening the array by one. array_shift()将数组的第一个值移开并返回它,将数组缩短一。

https://www.php.net/manual/en/function.array-shift.php https://www.php.net/manual/en/function.array-shift.php

Once the value is printed, it will be removed from the array.打印该值后,它将从数组中删除。 So no repeated values所以没有重复的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM