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),
        );
    }
}

?>
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Sarumahr
  • 13
  • 5

1 Answers1

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
Athene noctua
  • 82
  • 1
  • 7
  • Nice, I wrote this before the post was closed: http://sandbox.onlinephpfunctions.com/code/44900be2bbcc456fd1f9e41aa3e2e6908ba16345 legit embarrassed. Here's a thumbs up for you – zanderwar Aug 12 '20 at 06:48
  • The problem is that a lot of these things have been answered before - have a look at the answer at https://stackoverflow.com/a/5612704/1213708. – Nigel Ren Aug 12 '20 at 06:49
  • Before it was close :http://sandbox.onlinephpfunctions.com/code/73fc3c838a5d324406d170db927b67d4e23167f7 – BackTrack57 Aug 12 '20 at 06:50