0

I'm looking for a way to create an array with 6 random and unique ints. This is what I have so far but unfortunately it's not working correctly:

<?php
for ($i = 0; $i <= 5; $i++) {
    $values[$i] = array(
        rand($min = 1, $max = 10),
    );
    print_r($values[$i]);
    print "<br>";
    $values = array_unique($values);
    if ($values[$i] != null) {
        var_dump($values[$i]);
    } else {
        $values[$i] = array(
            rand($min = 1, $max = 10),
        );
    }
}

?>
1
  • print_r(str_split(substr(str_shuffle('0123456789'),1,6))); Commented Aug 12, 2020 at 6:49

1 Answer 1

2

You don't necessary need to use loops to achieve this, I would personally write it like this:

$nums = range(1,10);
shuffle($nums);
$values = array_slice($nums,0,6);
// now $values contains six random integers within the given range
Sign up to request clarification or add additional context in comments.

3 Comments

Nice, I wrote this before the post was closed: sandbox.onlinephpfunctions.com/code/… legit embarrassed. Here's a thumbs up for you
The problem is that a lot of these things have been answered before - have a look at the answer at stackoverflow.com/a/5612704/1213708.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.