Finding single array in multidimensional?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spmckee
Forum Newbie
Posts: 1
Joined: Wed Mar 10, 2010 11:10 am

Finding single array in multidimensional?

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Finding single array in multidimensional?

Post by AbraCadaver »

Here's a way:

Code: Select all

$Retrievers = $L1_array['Animals & People']['Animals']['Dogs']['Retrievers'];
 
foreach($Retrievers as $dog) {
    echo $dog;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply