smproph Posted January 24, 2011 Share Posted January 24, 2011 I have it to where it groups two parts of the Array randomly but I need to make it where it won't pair the same together, or if it has already been paired with a team then use another team. Each value of the array can only be used once. Here is my code shuffle($teamname); $loopCount = $games; for ($i = 0; $i < $loopCount; $i++) { $player1 = array_pop($teamname); $player2 = array_pop($teamname); if(is_null($player1)) { echo 'Not enough different teams to create '.$games.' games'; break; } // output to screen echo "Team: " . $player1 . " Vs Team: " . $player2 . "<br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/225552-group-random-array-elements/ Share on other sites More sharing options...
ManiacDan Posted January 24, 2011 Share Posted January 24, 2011 This is called the "handshake problem" (or, more broadly, permutations and combinations). Look it up on wikipedia, the read will benefit the way you think about algorithms. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/225552-group-random-array-elements/#findComment-1164648 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.