Jump to content

Recommended Posts

I want to pair names for projects so that everybody has to work with everybody. The hard part is setting the output so that a person doesn't have to go two or more times in a row.

 

I have the following code that outputs almost what i want with 9 people, if there are more or less i would have to change the manually made volgorde array, i would like it to become automatic :-).

 

I only figured out that the max possibilities works with the following math: 9+8+7+6+5+4+3+2+1. So i could use this with more or less names.

 

<?php

 

$namen=array(Bert,Dries,Frank,July,Evelien,Mark,Lies,Nico,Sofie);

shuffle($namen);  //I shuffle so Bert doesn't come out all the time as the first person :-)

 

$volgorde=array(0,15,26,33,7,9,16,31,34,1,9,27,35,4,8,21,30,6,14,23,10,19,32,3,13,25,17,5,28,22,20,12,2,29,18,11,24);

 

for ($i=0;$i<count($namen);$i++) {

$combinaam=$namen[$i];

$y=$i+1;

 

for (;$y<count($namen);$y++) $combinamen[]="$combinaam en $namen[$y]";

}

 

 

for ($i=0;$i<count($combinamen);$i++) {

$toon=$volgorde[$i];

echo"$i. $combinamen[$toon]<br>";

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/144495-pairing-names-for-projects/
Share on other sites

First off, all of those names in the namen array should be in quotes unless they actually are constants.

 

 

 

Now that that's out of the way:

 

 

You want an array of x names, and you want to make x/2 pairs with unique elements?

 

 

Why not just shuffle the array and then pair people who are next to each other in the array?

 

$names = array("Corbin", "John", "Bob", "Sally");

shuffle($names);

$pairs = array();

$c = count($names);

for($i = 0; $i < $c; $i++) {

    $pairs[] = array($names[$i], $names[++$i]);

}

This will generate an array of pairs where every person is grouped with all other persons exactly once.

 

<?php
$names = $namesCopy = array('Bert', 'Dries', 'Frank', 'July', 'Evelien', 'Mark', 'Lies', 'Nico', 'Sofie');

$pairs = array();

while (count($namesCopy)) {
$currentName = array_shift($namesCopy);
foreach ($namesCopy as $name) {
	$pairs[] = array($currentName, $name);
}
}

print_r($pairs);
?>

 

Basically the idea is that you create a copy array (or you could use the original array if you wish) and run through it. On each iteration you remove the first person and group that person with all the other persons. The reason why you remove him from the array is that he has already been placed in all the possible pairs he can be in. You keep doing that until there are no people left who have not been grouped.

 

Also, you got your math wrong. The number of combinations if you pick k people out of n people is given by:

[tex]C(n,k) = \frac{n!}{(n-k)! \cdot k!}[/tex]

 

So that gives:

[tex]C(9,2) = \frac{9!}{(9-2)! \cdot 2!} = 36[/tex]

 

Yours however is like this for n people:

[tex]C(n) = \sum_{i=1}^n i[/tex]

[tex]C(9) = \sum_{i=1}^9 i = 45[/tex]

 

I'm not sure where you got that from.

 

Finally, I recommend that you name all variables, functions, classes, etc. in English.

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.