I'm running through a great old brain fart currently and am stuck dynamically selecting the next "round match" that the winners of the below rounds will advance to:
The ladder above is dynamically generated, and what I'd like to do is figure out the next match ID. I've got this as a POC currently, but it isn't sustainable if a competition ladder were to run up to 64/more:
$ar = [
1 => [
['id' => 1,'name' => 'round1, pair 1'],
['id' => 2,'name' => 'round1, pair 2'],
['id' => 3,'name' => 'round1, pair 3'],
['id' => 4,'name' => 'round1, pair 4'],
],
2 => [
['id' => 5,'name' => 'round2, pair 1'],
['id' => 6,'name' => 'round2, pair 2'],
]
];
$cases = [0, 0, 1, 1, 2, 2];
foreach($ar as $i => $round) {
foreach($round as $_i => $r) {
echo $r['name'] . " & NEXT_MATCH_ID::> " . $ar[($i + 1)][$cases[$_i]]['id'] . "<br /> ";
}
}
Is there a more simplified way of achieving what the above without hard-coded variables ($cases
) for example.
Essentially the amount of "matches/pairs" are being halved as a ladder would be: 4
-> 2
-> 1
.
The above generates the correct ID's but it isn't expandable or dynamic;
round1, pair 1 & NEXT_MATCH_ID::> 5
round1, pair 2 & NEXT_MATCH_ID::> 5
round1, pair 3 & NEXT_MATCH_ID::> 6
round1, pair 4 & NEXT_MATCH_ID::> 6
round2, pair 1 & NEXT_MATCH_ID::> ...
round2, pair 2 & NEXT_MATCH_ID::> ...
//......etc etc...
Demo/ Example of the above code if required.
Notes
- There is no limit on "players/team" matches and this can be exponential,
4, 6, 8, 10, 12, 14, 16, 18....32, 34...64...etc
. - This will never happen/apply to the last round (Grand Final -
1
match) as there is no further round to advance to. (easily limited byif($i == count($rounds)) {.... do not continue...
). - There is a possibility of multiple matches being run simultaneously, so the "next round ID" could not be
lastId + 1
.