You probably just want to put the switch
statement into the foreach
?
foreach ($tables as $table) {
switch ($table) {
case 'table_one' :
// do something here
break;
case 'table_two' :
// do something here
break;
case 'table_three' :
// do something here
break;
default :
// do some error handling here
break;
}
}
Alternatively, a switch isn't that easy to read, consider going away from a switch
and using an array-powered if
, especially if you could dynamically create what you want to do each case
:
$tables = array('table_one', 'table_two', 'table_three');
if (in_array($table, $tables)) {
// do something here
} else {
// do some error handling here
}
That's a lot more readable, even if your array has a lot of elements.