Let's say the teams collection is an array.
array_shift() a team off the array.
Loop through the array, pairing each array value with the team you shifted off.
Repeat until count($array) == 1.
I'd stick that in a class, with an iterator interface, for example:
Code: Select all
$matches =& new Pairs($teams);
$matches->prepare();
while($matches->isValid())
{
// do something with $matches->next();
}
Maybe add some checks for the Pairs class array parameter:
(a) count($teams) is a multiple of two
(b) count($teams) is at least two
As it happens I was curious enough to knock something up. It's not the last word (prepare() should maybe be refactored) but it works.
Code: Select all
/*
CLASS Pairs
Get unique pairs from a collection of values.
*/
class Pairs
{
var $i = 0;
var $pairs = array();
var $collection;
/*
param (array)
*/
function Pairs($collection)
{
$this->collection = $collection;
}
function prepare()
{
if($this->_checkCollection())
{
$this->_setPairs();
$this->element = each($this->pairs);
} else {
$this->element = false;
}
reset($this->pairs);
}
/*
return (bool)
*/
function isValid()
{
return ($this->element !== false);
}
/*
return (array)
*/
function next()
{
$this->element = each($this->pairs);
return $this->element['value'];
}
//////////////////////////////////////////
// PRIVATE //
//////////////////////////////////////////
/*
return (bool)
*/
function _checkCollection()
{
$num_elements = count($this->collection);
if($num_elements > 1 and $num_elements % 2 == 0)
{
return true;
} else {
return false;
}
}
/*
build an array of unique $collection pairs
*/
function _setPairs()
{
while($current = array_shift($this->collection))
{
foreach($this->collection as $value)
{
$this->pairs[$this->i][] = $current;
$this->pairs[$this->i][] = $value;
$this->i++;
}
if(count($this->collection) == 1)
{
break;
}
}
}
}
///////////////
// END CLASS //
///////////////
The unit test (using SimpleTest - see link below). I strongly recommend you look at SimpleTest if you aren't already one of the test-infected.
Code: Select all
require_once('lib/simpletest/unit_tester.php');
require_once('lib/simpletest/reporter.php');
include('lib/iterators/pairs.php');
class TestOfPairs extends UnitTestCase
{
function TestOfPairs()
{
$this->UnitTestCase();
}
function testValidCollection()
{
$collection = array('a', 'b', 'c', 'd');
$pairs =& new Pairs($collection);
$pairs->prepare();
$this->assertTrue($pairs->isValid());
$this->assertEqual($pairs->next(), array('a', 'b'));
$this->assertEqual($pairs->next(), array('a', 'c'));
$this->assertEqual($pairs->next(), array('a', 'd'));
$this->assertEqual($pairs->next(), array('b', 'c'));
$this->assertEqual($pairs->next(), array('b', 'd'));
$this->assertEqual($pairs->next(), array('c', 'd'));
$pairs->next();
$this->assertFalse($pairs->isValid());
}
function testInValidCollectionWithCountLessThanTwor()
{
$collection = array('a');
$pairs =& new Pairs($collection);
$pairs->prepare();
$this->assertFalse($pairs->isValid());
}
function testInValidCollectionWithOddMemberCount()
{
$collection = array('a', 'b', 'c');
$pairs =& new Pairs($collection);
$pairs->prepare();
$this->assertFalse($pairs->isValid());
}
}
$test =& new TestOfPairs();
$test->run(new HtmlReporter());