0

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>
5
  • Have a look at shuffle() Commented Feb 25, 2020 at 19:14
  • So table needs to have unique values? Also, please indent your code Commented Feb 25, 2020 at 19:14
  • Thanks, I have tried, all could but not working! Commented Feb 25, 2020 at 19:19
  • 1
    When you reload the browser, there's nothing keeping track of anything from previous requests. You'll need to maintain that somehow, probably using session. Here's an example of something like that: stackoverflow.com/questions/51458831/… Commented Feb 25, 2020 at 19:19
  • Thanks, I tried it still duplicate. Commented Feb 25, 2020 at 19:26

1 Answer 1

0
$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

So each time you reload the page, the order will be different

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.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it works well, I really appreciate, you save my time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.