what's the best way to pair elements in an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

what's the best way to pair elements in an array

Post by s.dot »

Code: Select all

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

//shuffle 10 times
for($i=0; $i<10; $i++)
{
	shuffle($participants);
}

//make pairs
$pairs = array();
I need to pair them up. So I'll have 5 total pairs out of that array. What's the best way to do that?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I got this

Code: Select all

$j = 0;
for($i=0; $i<(count($participants)/2);$i++)
{
	$pairs[] = array($participants[$j], $participants[$j + 1]);
	$j = $j+2;
}
Didn't think of using array_chunk(). Mine just looked ugly so I thought there had to be a less messy way :P

Code: Select all

$pairs = array_chunk($participants, 2);
looks much better.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I assumed you want to randomize the pairing, which may or may not be the case... If you do, call shuffle($participants) first.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Kieran Huggins wrote:I assumed you want to randomize the pairing, which may or may not be the case... If you do, call shuffle($participants) first.
I did ;)

My script ended up like this.

Code: Select all

<?php

//array of all participants
$participants = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);

//random number to shuffle
$rand = mt_rand(10, 100);

//shuffle $rand times
for($i=0; $i<$rand; $i++)
{
	shuffle($participants);
}

//make pairs
$pairs = array_chunk($participants, 2);

//list pairs
foreach($pairs AS $pair)
{
	echo '<p>'.$pair[0].'<br />'.$pair[1].'</p>';
}

?>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

//random number to shuffle
$rand = mt_rand(10, 100);

//shuffle $rand times
for($i=0; $i<$rand; $i++)
{
        shuffle($participants);
}
Whats the point? Simply call shuffle() once and let that be the end of it ;)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

adds a bit of randomness?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

shuffling once is no more random than shuffling a billion times.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Ah, I didn't know that.

I figured it was like a deck of cards. When you first open them up, they're in order. Shuffle once and they'll be randomized, but some still in order.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

nope, but if you want to use the shuffling of a deck of cards analogy, imagine throwing a deck of cards in the air and picking them up after.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Cool.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

$array = pickup('52');
Post Reply