array switch case statement

Asim Zaidi picture Asim Zaidi · Apr 29, 2011 · Viewed 26.5k times · Source

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

Answer

iandouglas picture iandouglas · Apr 30, 2011

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 ;
     }
 }