Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中从数组生成开关用例?_Php_Foreach_Switch Statement - Fatal编程技术网

在php中从数组生成开关用例?

在php中从数组生成开关用例?,php,foreach,switch-statement,Php,Foreach,Switch Statement,是否可以使用数组为php中的开关生成案例?比如: $x=array( 0 => 'foo', 1 => 'bar', 2 => 'foobar' ); $y='foobar' switch($y) { foreach($x as $i) { case $i: print 'Variable $y tripped switch: '.$i.'<br>'; break;

是否可以使用数组为php中的开关生成案例?比如:

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

$y='foobar'

switch($y) {
    foreach($x as $i) {
        case $i:
            print 'Variable $y tripped switch: '.$i.'<br>';
            break;
    }
}
$x=array(
0=>“foo”,
1=>bar',
2=>“foobar”
);
$y='foobar'
交换机($y){
外汇($x作为$i){
案例一:
打印“变量$y跳闸开关:”.$i.
”; 打破 } }

我希望能够从数据库中提取大小写值,并使用while()循环遍历它们。

否。开关是开关,但您可以使用数组键选择正确的值。基本上,在数组中,您可以使键和值相同,然后您可以使用if函数,如下所示:

if ($array[$key]) ....
如果($array[$key])…我相信您要寻找的是与此类似的东西

foreach ($x as $i) {
    switch($i){
        case $y:
            print 'Variable $x tripped switch: '.$i.'<br>';
            break;
    }
}
foreach($x作为$i){
交换机(一美元){
案例$y:
打印“可变$x跳闸开关:”.$i.
”; 打破 } }
否。您想用它做什么?为什么你认为你需要一个开关/机箱?无论你想做什么,
switch
都会使它过于复杂。你想要的是不可能的,并且有很好的理由-代码可以简单地写成
foreach($x为$i){if($i===y){print'变量$y触发开关:'.$i.
;}
(假设你在代码示例中混淆了
$x
),我其实并不需要它,这只是我出于猜测而修补的东西。