TEENFRONT Posted January 17, 2010 Share Posted January 17, 2010 Hey guys Looking for some quick help if at all possible. Basically, i have a bunch of numbers in my database all seperated with a comma. So my "player" field has this in it 1245, 56473, 484933, 929121, 040485, 03303 etc, just a bunch of "player IDs"... Basically i want to put these IDs into an array (i think) by seperating the commas. Then, the main thing, is i want to pair up the player IDs with out ones, random. Like "PLAYERID VS. PLAYERID" i dont fully understand arrays if im honest.. i know how to randomize them and echo out them in a random order, but i need to pair each seperate id up with another id. I just cannot get my head round how to do it. hoping i explained ok, its 7 in the morning and iv been awake all night lol. Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/188762-randon-pair-up-of-array-items/ Share on other sites More sharing options...
oni-kun Posted January 17, 2010 Share Posted January 17, 2010 $ids = '1245, 56473, 484933, 929121, 040485, 03303'; //From DB row $playerIDs = explode(', ' , $ids); //Explode to clean array For starters.. Are you wanting to pair the ID's with eachother, or is there another set of players? Quote Link to comment https://forums.phpfreaks.com/topic/188762-randon-pair-up-of-array-items/#findComment-996494 Share on other sites More sharing options...
TEENFRONT Posted January 17, 2010 Author Share Posted January 17, 2010 Each ID needs pairing with another from the same set of IDs. So in your starter example each of the $playerIDs items needs pairing with another $playerIDs I basically want it to have an end output of "$playerIDs[random] VS. $playerIDs[random]" but once 2 IDs have been paired, they cannot be paired again with other IDs. I'm trying to create like a match roster. Quote Link to comment https://forums.phpfreaks.com/topic/188762-randon-pair-up-of-array-items/#findComment-996511 Share on other sites More sharing options...
Fergal Andrews Posted January 17, 2010 Share Posted January 17, 2010 This example takes the Id's as a string. I you are getting the Ids from a database the result set will already be an array, so skip to the Randomize array part of the code. The array created in this example will be an uneven number so someone will not get to play, but that is unavoidable. <?php $playerIds = '3847, 3795, 80880, 3453, 56456, 27431, 82903, 17879, 789222, 9839832, 90823432, 234234, 1232323'; $playerIdsArray = explode(',' ,$playerIds); // Randomize array shuffle($playerIdsArray); $loopCount = floor(count($playerIdsArray) / 2); for ($i = 0; $i < $loopCount; $i++) { $player1 = array_pop($playerIdsArray); $player2 = array_pop($playerIdsArray); // output to screen echo "Player Id: " . $player1 . " Vs Player Id: " . $player2 . "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188762-randon-pair-up-of-array-items/#findComment-996626 Share on other sites More sharing options...
salathe Posted January 17, 2010 Share Posted January 17, 2010 You could also use array_chunk to split the list of player IDs into pairs. Like: $list = '3847, 3795, 80880, 3453, 56456, 27431, 82903, 17879, 789222, 9839832, 90823432, 234234, 1232323'; // Split into array of IDs $players = explode(', ', $list); // Shuffle the array shuffle($players); // Chunk into pairs $pairs = array_chunk($players, 2); foreach ($pairs as $pair) { // If not pair (i.e. list had uneven number of IDs) skip if ( ! isset($pair[1])) { continue; } printf("Player Id: %d Vs Player Id: %d\n", $pair[0], $pair[1]); } Quote Link to comment https://forums.phpfreaks.com/topic/188762-randon-pair-up-of-array-items/#findComment-996643 Share on other sites More sharing options...
TEENFRONT Posted January 17, 2010 Author Share Posted January 17, 2010 Hey guys thanks for this. Il test it as soon as I wake up properly and il report back! Both Look great though! Quote Link to comment https://forums.phpfreaks.com/topic/188762-randon-pair-up-of-array-items/#findComment-996659 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.