I have this function that does what it's supposed to do. However, as I read it, it just seems like there's no way it's the easiest way. It also has a certain shortcoming - it has to receive an array that has to be in the correct order, even though it uses named keys. Otherwise, it sometimes produces incorrect results.
The function is for analyzing a team's performance in an elimination bracket and outputting how well they did - i.e. "Quarterfinalists". Here's the function:
function pud_elims_level($organizedarray,$teamnumber)
//gets the highest eliminations level at which a particular team competed - i.e. Quarterfinalists, Semifinalists, etc.
{//Ensure that the array entered into this function is organized IN THE ORDER qtr1,qtr2,...,semi1,semi2,final1 or it will not work correctly
$organizedarray = array_reverse($organizedarray, true); //reverses the keys in the organizedarray, so that it checks finals first
foreach($organizedarray as $k => $v)
{
$matchgroup=$v;
$lastmatch=end($matchgroup);
if($k=="final1") //a special check only done for the finals
{
switch($teamnumber)
{
case $lastmatch[2]:
case $lastmatch[3]:
case $lastmatch[4]:
if($lastmatch[8]>=$lastmatch[9])
{
return "Champions";
}
break;
case $lastmatch[5]:
case $lastmatch[6]:
case $lastmatch[7]:
if($lastmatch[9]>=$lastmatch[8])
{
return "Champions";
}
break;
}
}
switch($teamnumber)
{//these are the six values in which team numbers are stored
case $lastmatch[2]:
case $lastmatch[3]:
case $lastmatch[4]:
case $lastmatch[5]:
case $lastmatch[6]:
case $lastmatch[7]:
switch(rtrim($k,"1234"))//checks agianst $k WITHOUT the number - so k can only be qtr, semi, or final
{
case "qtr":
return "Quarterfinalists";
case "semi":
return "Semifinalists";
case "final":
return "Finalists";
}
break;
}
}
return false;
}
The data is stored in a multi-dimensional arrray. The first level holds the set. The keys are: qtr1...qtr4, semi1, semi2, and final1. Each key then contains an array of individual matches - each set is a best-out-of-three. Here's an example of the array below
$array=array(
'qtr1'=>array( //quarterfinals set 1
0 => array(
//information about the specific match here - team numbers are in keys 2-7
),
1 => array(
//the next match of set 1 here
),
),
'qtr2'=>array(
0 => array(
//The first match of quarterfinals set 2 here
),
1 => array(
//the next match of quarterfinals set 2 here
),
1 => array(
//had to go on to a third match, qtr2-3
),
),
//etc etc etc
);
The above function works by first inverting the order of the array so that it begins with final1, then checks if the specified team competed in final1. If it did, it checks if it won or lost. If it won, it returns "Champions". If it didn't, then it goes through the finals, semifinals, and quarterfinals, looking for the first (highest) occurrence of the team, and returns the appropriate value. However, if the initial array had, say, final1 BEFORE qtr1, then when it got reversed it would be after, which would mean that if a team competed in final1 it would still return "Quarterfinalists" because it saw that the team was in qtr1 and stopped.
Is there a better way? It seems like the entire methodology behind the script is terrible, and I wrote it. It works for me right now, because the function that generates the array puts it in the correct order every time, but it's for a library that is to be distributed, and who knows what other people will do.