Jump to content

Randon Pair up of array items?


TEENFRONT

Recommended Posts

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!!

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />"; 

   

}

 

?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.