DCR DCR - 2 months ago 12
PHP Question

How to traverse a transition matrix

I have a transition matrix that is 5X5. Basically old state going to new state. I've set up the following if statement block but was wondering if there is a more elegant solution:

if( ($oldStatus[$i] == "OUT" || $oldStatus[$i]=="OPEN") && $newStatus[$i] == "OUT"){
$email[$i] = false;
$crw [$i] = 'OUT';
$sort [$i] = 9;
}else if ( ($oldStatus[$i]=="OUT" || $oldStatus[$i]=="OPEN") && $newStatus[$i]=="CONFIRMED"){


}else if ( ($oldStatus[$i]=="OUT" || $oldStatus[$i]=="OPEN") && $newStatus[$i]=="WAITLIST"){


}else if ( ($oldStatus[$i]=="OUT" || $oldStatus[$i]=="OPEN") && $newStatus[$i]=="FLEXOPEN"){


}else if ( ($oldStatus[$i]=="OUT" || $oldStatus[$i]=="OPEN") && $newStatus[$i]=="FLEXCLOSE"){


}else if ($oldStatus[$i]=="CONFIRMED" && $newStatus[$i]=="OUT"){


}else if ($oldStatus[$i]=="CONFIRMED" && $newStatus[$i]=="CONFIRMED"){


}else if ($oldStatus[$i]=="CONFIRMED" && $newStatus[$i]=="WAITLIST"){


}else if ($oldStatus[$i]=="CONFIRMED" && $newStatus[$i]=="FLEXOPEN"){


}else if ($oldStatus[$i]=="CONFIRMED" && $newStatus[$i]=="FLEXCLOSE"){


}else if ($oldStatus[$i]=="WAITLIST" && $newStatus[$i]=="OUT"){


}else if ($oldStatus[$i]=="WAITLIST" && $newStatus[$i]=="CONFIRMED"){


}else if ($oldStatus[$i]=="WAITLIST" && $newStatus[$i]=="WAITLIST"){


}else if ($oldStatus[$i]=="WAITLIST" && $newStatus[$i]=="FLEXOPEN"){


}else if ($oldStatus[$i]=="WAITLIST" && $newStatus[$i]=="FLEXCLOSE"){


}else if ($oldStatus[$i]=="FLEXOPEN" && $newStatus[$i]=="OUT"){


}else if ($oldStatus[$i]=="FLEXOPEN" && $newStatus[$i]=="CONFIRMED"){


}else if ($oldStatus[$i]=="FLEXOPEN" && $newStatus[$i]=="WAITLIST"){


}else if ($oldStatus[$i]=="FLEXOPEN" && $newStatus[$i]=="FLEXOPEN"){


}else if ($oldStatus[$i]=="FLEXOPEN" && $newStatus[$i]=="FLEXCLOSE"){


}else if ($oldStatus[$i]=="FLEXCLOSE" && $newStatus[$i]=="OUT"){


}else if ($oldStatus[$i]=="FLEXCLOSE" && $newStatus[$i]=="CONFIRMED"){


}else if ($oldStatus[$i]=="FLEXCLOSE" && $newStatus[$i]=="WAITLIST"){


}else if ($oldStatus[$i]=="FLEXCLOSE" && $newStatus[$i]=="FLEXOPEN"){


}else if ($oldStatus[$i]=="FLEXCLOSE" && $newStatus[$i]=="FLEXCLOSE"){


}

Answer

You can operate through switches... something like

switch($oldStatus[$i]) {
   case 'OPEN':
   case 'OUT':
        switch($newStatus[$i]) {
            case 'CONFIRMED':
                 //do something here
            break;
            case 'WAITLIST' {
                 //do something else here
            break;
         }
    break;
    case 'CONFIRMED':
        switch($newStatus[$i]) {
            case 'CONFIRMED':
                 //do something here
            break;
            case 'WAITLIST' {
                 //do something else here
            break;
            //do the rest of the new cases here
         }
    break;
    //do the rest of the old cases just like above here
}

Makes it a little prettier and easier to manage in the future if there are new options

Comments