Assign 2 Numbers to each player from seperate array

Viewed 42

I have 2 PHP Arrays, one is filled with players names

$players = array("Player 1", "Player 2", "Player 3", 
                 "Player 4", "Player 5");

Another array is filled with numbers

$numbers= array("1", "2", "3", "4", "5", 
                "6", "7", "8", "9", "10");

I'm trying to figure out how i can assign 2 random numbers to each player

This is where i'm at so far but it's not doing what i want it to.

foreach($players as $player) {
    shuffle($keys);
    echo $player." 2 Selections:<br />";
    echo $numbers[$keys[0]] . ',' . $numbers[$keys[1]];
    echo "<br /><br />";
    unset($numbers[$keys[0]]);
    unset($numbers[$keys[1]]);
    array_values($numbers);
}
4 Answers

Something like this:

$players = array("Player 1", "Player 2", "Player 3", "Player 4", "Player 5");

$numbers = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
// shuffle numbers just once
shuffle($numbers);

foreach($players as $player) {
    echo $player." 2 Selections:<br />";
    // `array_pop` takes the last element of array 
    // and reduces array at the same time
    echo array_pop($numbers) . ';' . array_pop($numbers);
    echo "<br /><br />";
}

See the fiddle.

Maybe by simply shuffling once and then not reusing the same Value like this

$players = array("Player 1", "Player 2", "Player 3", 
                 "Player 4", "Player 5");

$numbers= array("1", "2", "3", "4", "5", 
                 "6", "7", "8", "9", "10");
shuffle($numbers);
$nid = 0;

foreach($players as $player) {
    echo $player." 2 Selections:<br />";
    echo $numbers[$nid] . ',' . $numbers[$nid + 1];
    echo "<br /><br />";
    $nid += 2;
}

RESULTS

Player 1 2 Selections:<br />10,3<br /><br />
Player 2 2 Selections:<br />4,5<br /><br />
Player 3 2 Selections:<br />2,7<br /><br />
Player 4 2 Selections:<br />1,6<br /><br />
Player 5 2 Selections:<br />8,9<br /><br />

Also using this, the $numbers array is not actually destroyed and could be reused later if that was necessary.

There are many ways to achieve your aim, but your attempt is very close to working. The only problem is that you haven't actually defined $keys anywhere. If you define it at the beginning of the foreach loop based on the remaining items in $numbers (with array_values), it will work as desired:

foreach($players as $player) {
    $keys = array_keys($numbers);
    shuffle($keys);
    echo $player." 2 Selections:<br />";
    echo $numbers[$keys[0]] . ',' . $numbers[$keys[1]];
    echo "<br /><br />";
    unset($numbers[$keys[0]]);
    unset($numbers[$keys[1]]);
}

Note that I've removed the last line array_values($numbers); because it doesn't do anything - array_values returns a new array, it doesn't modify it in place. You probably meant to write $numbers = array_values($numbers); but don't actually need to if you use array_keys as above.

Other approaches to the problem include:

  • Shuffling the $numbers array directly, and using array_pop or array_shift to take elements from it.
  • As above, but using a counter that you add 1 to each time rather than modifying the list, e.g. $pick = $numbers[$i++];
  • Using the array_rand function to pick the keys, and the same unset you have.

I have not seen any $key but I assume you already have it somewhere. Please, next time show us the error message. ;)

If you ask for two random numbers, use rand() to make your intent clear:

$players = array("Player 1", "Player 2", "Player 3", 
                 "Player 4", "Player 5");
                
$dict = [];
foreach($players as $player)
{
    $numbers = [rand(1, 10), rand(1, 10)];
    $dict[$player] = $numbers;
}

But I think what you want is two randomly selected numbers so that no player share a number with another:

$players = array("Player 1", "Player 2", "Player 3", 
                 "Player 4", "Player 5");
             
$numbers = [1,2,3,4,5,6,7,8,9,10];
shuffle($numbers);

$dict = [];
foreach($players as $player)
{
   $two_numbers = [array_pop($numbers), array_pop($numbers)];
   $dict[$player] = $two_numbers;
}

Display Results:

foreach($players as $player)
{
    echo $player."\n";
    echo $dict[$player][0]."\n";
    echo($dict[$player][1]."\n");
}

EDIT: u_mulder was first when proposing "two randomly selected numbers". Just give it to him. FIFO!

Related