Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array switch case statement

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

so something like this

switch($array){

    case Products:

    case customers:

    case Models:
}

how would I do that. Thanks

like image 928
Asim Zaidi Avatar asked Apr 29 '11 21:04

Asim Zaidi


People also ask

Can we use switch case in array?

Yes, you can pass an array to a switch.

Can we use array in switch case in C?

In C, you cannot use arrays in switch (and expressions for case ). Also, the type passed to switch() and types specified in each case must match.

What is the statement of switch case?

Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used to perform different actions based on different conditions(cases).


1 Answers

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]

foreach ($array[0] as $key => $value) {
    switch ($key) {
        case 'Products' :
            // do something
            break ;
        case 'customers' :
            // do something
            break ;
        case 'Models' :
            // do something
            break ;
     }
 }
like image 86
iandouglas Avatar answered Oct 28 '22 17:10

iandouglas