If we had four teams we could write out all possible match permutations like this:
(1, 1) (1, 2) (1, 3) (1, 4)
(2, 1) (2, 2) (2, 3) (2, 4)
(3, 1) (3, 2) (3, 3) (3, 4)
(4, 1) (4, 2) (4, 3) (4, 4)
You can see the repeats mirrored in the diagonal.
I used the following code to output the above:
for($i=1; $i<5; $i++) {
for($j=1; $j<5; $j++) {
echo "($i, $j) ";
}
echo "\n";
}
If we only care about the couplets where $i is less than $j. Or alternatively where $i is greater than $j, we weed out the duplicates, and the diagonal itself (where the teams play themselves).
function team_combinations($no_teams) {
$combinations = array();
for($i=1; $i<=$no_teams; $i++) {
for($j=1; $j<=$no_teams; $j++) {
if($i < $j)
$combinations[] = array($i, $j);
}
}
return $combinations;
}
var_export(team_combinations(4));
Output:
array (
0 =>
array (
0 => 1,
1 => 2,
),
1 =>
array (
0 => 1,
1 => 3,
),
2 =>
array (
0 => 1,
1 => 4,
),
3 =>
array (
0 => 2,
1 => 3,
),
4 =>
array (
0 => 2,
1 => 4,
),
5 =>
array (
0 => 3,
1 => 4,
),
)