# Need help with logic, create a three-dimensional array (PHP)

I need help, example, i got array with 4 for team names there.

```array('logiX.L4d22','Lust','Marat and Friends','Pandas of Belgium');
```

I want to make 3 dimensional array where first is a round, second is a match, and the third is teams who plays with eachother, there will be always 2 teams.

the logic must make that all teams must be played with all others and in one round, any team will play only one match, so if we got 5 teams then in some round one team must just wait to the next round.

it must produce something like this:

```array
0 =>
array
0 =>
array
0 => string 'logiX.L4D2' (length=10)
1 => string 'Lust' (length=4)
1 =>
array
0 => string 'Marat and Friends' (length=17)
1 => string 'Pandas of Belgium' (length=17)
1 =>
array
0 =>
array
0 => string 'logiX.L4D2' (length=10)
1 => string 'Marat and Friends' (length=17)
1 =>
array
0 => string 'Lust' (length=4)
1 => string 'Pandas of Belgium' (length=17)
2 =>
array
0 =>
array
0 => string 'logiX.L4D2' (length=10)
1 => string 'Pandas of Belgium' (length=17)
1 =>
array
0 => string 'Lust' (length=4)
1 => string 'Marat and Friends' (length=17)
```

It must be work with 2,3,5 ...10 ...12 teams.

I hope that you can help me out, i already spent 1 and half days for that.

Some Googling on Round Robin algorithm PHP gives the following:

http://speedtech.it/blog/2009/03/15/round-robin-algorithm-php/

I hope you'll find what you're looking for.

EDIT

Adding my attempt for this, following the round-robin algorithm described on Wikipedia.

If teams number is odd, it adds a team in the array (null value), so you can retrieve the "waiting team" for each round.

``````<?php

\$teams = range('a', 'g');

function make_rounds(\$teams)
{
\$nb_teams = count(\$teams);

if (\$nb_teams % 2 != 0)
{
\$teams[] = null;
\$nb_teams++;
}

\$nb_rounds = \$nb_teams - 1;
\$nb_matches = \$nb_teams / 2;

\$rounds = array();

for(\$round_index = 0; \$round_index < \$nb_rounds; \$round_index++)
{
\$matches = array();

for(\$match_index = 0; \$match_index < \$nb_matches; \$match_index++)
{
if (\$match_index == 0)
\$first_team = \$teams;
else
\$first_team = \$teams[((\$nb_teams-2) + \$match_index - \$round_index) % (\$nb_teams-1) + 1];

\$second_team = \$teams[((\$nb_teams*2) - \$match_index - \$round_index - 3) % (\$nb_teams-1) + 1];

\$matches[] = array(\$first_team, \$second_team);
}

\$rounds[] = \$matches;
}

return \$rounds;
}

print_r(make_rounds(\$teams));
```
```