Generate switch cases in php from an array?

Posted by mopsyd on Stack Overflow See other posts from Stack Overflow or by mopsyd
Published on 2012-12-17T22:45:56Z Indexed on 2012/12/17 23:03 UTC
Read the original article Hit count: 155

Filed under:
|
|

Is it possible to generate the cases for a switch in php using an array? Something like:

$x=array(
    0 => 'foo',
    1 => 'bar',
    2 => 'foobar'
);

$y='foobar'

switch($y) {
    foreach($x as $i) {
        case $x:
            print 'Variable $y tripped switch: '.$i.'<br>';
            break;
    }
}

I would like to be able to pull the case values from a database and loop through them with a while() loop.

© Stack Overflow or respective owner

Related posts about php

Related posts about foreach