1

Here is my code. I don't understand why am I in endless loop. I think $check has to stop the loop when I make unique random values for my array.

    <?php

        $foo["blue"] = 0;
        $foo["black"] = 0;
        $foo["red"] = 0;
        $foo["white"] = 0;

        $check;
        do
        {
            foreach($foo as &$val)
            {
                $val = rand(1,6);
            }

            $foo = array_unique($foo);
            $check = count($foo);

        }
        while($check != 4);

        echo '............................ <br>';
        foreach($foo as $key=>$value)
        {
            echo $key . ' ' . $value . '<br>';
        }

    ?>
3
  • Try putting print_r($foo); in the do-while loop to see what the values are. Commented Sep 28, 2017 at 22:04
  • @ishegg That's OK, it's how you update the current array element in a foreach loop. Commented Sep 28, 2017 at 22:07
  • $foo = array_unique($foo). If, after the first iteration, the array has duplicated values, then that line will make $foo an array of three or less elements, and thus $check will always be different than 4, thus the loop will be infinite. The only way the loop will stop is if you get four different random values after the first iteration. Commented Sep 28, 2017 at 22:08

2 Answers 2

1

The problem is that the first time through the loop there are some duplicates, so array_unique() reduces the array from 4 elements to 1, 2, or 3. The foreach loop can never make the array bigger again, because it's only looping over the elements that currently exist in the array. So once the array shrinks, it will never grow back to 4 elements, and $check != 4 will always be true.

You should get the original keys of the array and use that.

<?php

    $foo["blue"] = 0;
    $foo["black"] = 0;
    $foo["red"] = 0;
    $foo["white"] = 0;
    $keys = array_keys($foo);

    $check;
    do
    {
        foreach($keys as $i)
        {
            $foo[$i] = rand(1,6);
        }

        $foo = array_unique($foo);
        $check = count($foo);

    }
    while($check != 4);

    echo '............................ <br>';
    foreach($foo as $key=>$value)
    {
        echo $key . ' ' . $value . '<br>';
    }

?>

DEMO

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

Comments

0

Personally, I wouldn't do it in a loop with such low entropy as it could take all day to finally match a random set.

A better way would be to generate a range and randomise that, then loop over your array and set the value.

<?php
$rand = range(1, 6);
shuffle($rand);

$foo["blue"] = 0;
$foo["black"] = 0;
$foo["red"] = 0;
$foo["white"] = 0;

$i=0;
foreach ($foo as $key => $value) {
    $foo[$key] = $rand[$i];
    $i++;
}

print_r($foo);
/*Array
(
    [blue] => 1
    [black] => 3
    [red] => 2
    [white] => 5
)
*/

https://3v4l.org/4hPku

Comments

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.