Il y a peut être une solution plus simple à mettre en place.
Si on veut créer tous les matchs d'une saison d'un seul coup (pas de match retour) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
<?php
$joueurs = [0 => 'Thomas', 1 => 'Romain', 2 => 'Pierre', 3 => 'Jean', 4 => 'Paul', 5 => 'Jacques'];
$journees = null;
$teamCount = count($joueurs);
if($teamCount < 2) {
return [];
}
if($teamCount % 2 === 1) {
array_push($joueurs, 'PAS DE MATCH');
$teamCount += 1;
}
$halfTeamCount = $teamCount / 2;
if($journees === null) {
$journees = $teamCount - 1;
}
$schedule = [];
for($journee = 1; $journee <= $journees; $journee += 1) {
foreach($joueurs as $key => $joueur) {
if($key >= $halfTeamCount) {
break;
}
$j1 = $joueur;
$j2 = $joueurs[$key + $halfTeamCount];
//Home-away swapping
$matchup = $journee % 2 === 0 ? [$j1, $j2] : [$j2, $j1];
$schedule[$journee][] = $matchup;
}
rotate($joueurs);
}
echo "<pre>"; echo var_dump($schedule); echo "</pre>";
function rotate(array &$items)
{
$itemCount = count($items);
if($itemCount < 3) {
return;
}
$lastIndex = $itemCount - 1;
$factor = (int) ($itemCount % 2 === 0 ? $itemCount / 2 : ($itemCount / 2) + 1);
$topRightIndex = $factor - 1;
$topRightItem = $items[$topRightIndex];
$bottomLeftIndex = $factor;
$bottomLeftItem = $items[$bottomLeftIndex];
for($i = $topRightIndex; $i > 0; $i -= 1) {
$items[$i] = $items[$i - 1];
}
for($i = $bottomLeftIndex; $i < $lastIndex; $i += 1) {
$items[$i] = $items[$i + 1];
}
$items[1] = $bottomLeftItem;
$items[$lastIndex] = $topRightItem;
} |
Ce code va te donner en résultat un tableau 3 dimension. -> $schedule
Au premier niveau seront tes journées, au second les matchs et au dernier les joueurs.
On obtient donc pour mon exemple de 6 joueurs :
- 5 journées de 3 matchs chacune
J'ai adapté un bout de code du github que je t'ai donné dans ma première réponse.
Tu peux maintenant remplacer cela tes objets

Partager