Page 1 of 1

Finding single array in multidimensional?

Posted: Wed Mar 10, 2010 11:12 am
by spmckee
I am creating a navigation hierarchy out of a multidimensional array and am having a terrible time trying to pull a single array out. Here's the code:

Code: Select all

<?
 
$L1_array = array( 'Colors & Shapes' => array (
                                                                   'Colors' => array (
                                        'Orange',
                                        'Blue',
                                        'White',
                                        'Red'
                                        ),
                                'Shapes' => array(
                                        'Circle',
                                        'Square',
                                        'Triangle'
                                        ),
                                ),
                'Animals & People' => array (
                                'Animals' => array (
                                        'Ducks',
                                        'Snails',
                                        'Dogs' => array (
                                                'Huskies',
                                                'Chows',
                                                'Retrievers' => array (
                                                        'Golden Retriever',
                                                        'Chesapeake Bay Retriever',
                                                        'Curly-Coated Retriever',
                                                        'Flat-Coated Retriever',
                                                        'German Water Spaniel',
                                                        'Golden Retriever',
                                                        'Irish Water Spaniel'
                                                        ),
                                'People' => array (
                                        'Russians',
                                        'Canadians',
                                        'Germans'
                                        ),
                                    ),
                                )
?>
For instance, how can I pull out and loop through JUST the retrievers array? Any help is greatly appreciated!

Thanks!

Re: Finding single array in multidimensional?

Posted: Wed Mar 10, 2010 1:19 pm
by AbraCadaver
Here's a way:

Code: Select all

$Retrievers = $L1_array['Animals & People']['Animals']['Dogs']['Retrievers'];
 
foreach($Retrievers as $dog) {
    echo $dog;
}