php 数组 switch case 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5837674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:38:11  来源:igfitidea点击:

array switch case statement

phpcakephp-1.3

提问by Asim Zaidi

I have an array coming in with sub arrays like this

我有一个带有这样的子数组的数组

Array
(
    [0] => Array
        (
            [customers] => Array
                (
                    [id] => 

                )

            [Products] => Array
                (
                    [id] => 

                )

            [Models] => Array
                (
                    [id] => 151


                    [SubModels] => Array
                        (
                            [ol] => 
                        )

                    [Noice] => 
                )

        )

I want to make a switch statement on the array

我想在数组上做一个 switch 语句

so something like this

所以像这样

switch($array){

    case Products:

    case customers:

    case Models:
}

how would I do that. Thanks

我该怎么做。谢谢

回答by iandouglas

since $array holds an array within it, it looks like you'll actually want to look at the keys of the array indexed at $array[0]

由于 $array 在其中包含一个数组,看起来您实际上想要查看以 $array[0] 为索引的数组的键

foreach ($array[0] as $key => $value) {
    switch ($key) {
        case 'Products' :
            // do something
            break ;
        case 'customers' :
            // do something
            break ;
        case 'Models' :
            // do something
            break ;
     }
 }