Generate clash keys between teams using an array in PHP

1

Oops !!

I have the following array in php , I need to create a clash between the teams and the teams that are of the same group / example array Time 01 and Time 02 can not face in the first round

I thought of checking if the element is from the same array if it takes one and passes it to the next, but I could not do that check. I do not know if there is already a function that does this !!

If anyone has a how-to tip, 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"
 }
}
    
asked by anonymous 05.06.2016 / 04:24

1 answer

0

If I understand you, you want to set all matches without repetition between teams from different groups .

This pairing can be obtained by looping all times from the beginning ($i=1,...,n) to a second loop that cycles through all times from the first loop forward ($j=$i,...,n) .

I recommend you try to do it, as it is a simple task and, 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
    
11.06.2016 / 07:32