Generate match keys between teams using a PHP array

Asked

Viewed 133 times

1

Opa!!

I have the following array on php, need to create a confrontation between teams and teams that are from the same group/array example Time 01 and Time 02 can not face each other in the first round.

I thought to check if the element are from the same array if yes takes one and passes to the next, only I could not do this check. I don’t know if there’s a function for that yet!!

If anyone has a tip on how to do I’m grateful!!

array(3) {
  [0]=>
  array(2) {
    [2]=>
    string(7) "Time 01"
    [3]=>
    string(7) "Time 02"
  }
  [1]=>
  array(1) {
    [0]=>
    string(7) "Time 03"
  }
  [2]=>
  array(1) {
    [1]=>
    string(7) "Time 04"
 }
}

1 answer

0

If I understand correctly you want to define all the pranks without repetition between teams of groups different.

This pairing can be achieved by having a loop run through all teams from the start ($i=1,...,n) and a second loop that traverses all teams from the first loop on ($j=$i,...,n).

I recommend you try to do, because it is a simple task and, only if you do not, look at the following solution:

$grupo1 = array("Time 01","Time 02");
$grupo2 = array("Time 03");
$grupo3 = array("Time 04");
$array_grupos = array($grupo1,$grupo2,$grupo3);
$n_de_grupos = count($array_grupos);    


$jogos= array();
for($i=0;$i<$n_de_grupos;$i++){
    for($j=$i+1;$j<$n_de_grupos;$j++){
        $grupoA = $array_grupos[$i];
        $grupoB = $array_grupos[$j];

        foreach($grupoA as $timeA){
            foreach($grupoB as $timeB){
                echo $timeA.$timeB.'<br>'; //imprime na tela 
                $jogos[]=array($timeA,$timeB);  //ou armazena em vetor
            }
        }
    }
}

//saída do echo:
Time 01Time 03
Time 02Time 03
Time 01Time 04
Time 02Time 04
Time 03Time 04
  • Oops!! Thank you for answering!! But in fact it’s the other way around the teams of the same group (array input) cannot face each other in the first round!! But for your example I will try to do here!!

  • Well, in my example the teams of the same group do not face, the opposite would be if only members of the same group faced each other....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.