Finding a string in a multidimensional array

I have a 4 level multidimensional array, and want to check if a string exists in the 4th level.
I want something like in_array(), but looking into the deepest level. I can’t work out how to do it.
I’m already in a foreach on the first level of the array, so am searching 3 levels down.
It’s hard to explain without giving a whole load of code.
It’s for a menu, where I want to apply an id to the current section.
This is the site, so you can see the menu and its html.
Here is the array that makes the menu:-

            $pg = basename($_SERVER['PHP_SELF']); 
            $menuarr = array(
                        'home' =>  array(
                                    'title' => 'Homepage',
                                    'icon' => 'home',
                                    'target' => '/',
                                    'sub' => null
                        'war' => array(
                                    'title' => 'Burton at War',
                                    'icon' => 'swords',
                                    'target' => '#war',
                                    'sub' => array(
                                                array('tankramp.php', 'The Tank Ramp'),
                                                array('location.php', 'Location'),
                                                array('thesite.php', 'The Site'),
                                                array('rollofhonour.php', 'Roll of Honour'),
                                                array('canadians.php', 'The Canadians')
                        'heritage' => array(
                                    'title' => 'Burtons Heritage',
                                    'icon' => 'heritage',
                                    'target' => '#heritage',
                                    'sub' => array(
                                                array('shipping.php', 'Shipping at Burton'),
                                                array('shipping.php?content=list', 'The Ship List'),
                                                array('brickworks.php', 'The Brickworks'),
                                                array('artefacts.php', 'Artefacts'),
                                                array('burtonwmc.php', 'Burton WMC'),
                                                array('history_cal.php', 'History Calendar')
                        'events' => array(
                                    'title' => 'BSHG Events',
                                    'icon' => 'calendar',
                                    'target' => '#events',
                                    'sub' => array(
                                                array('calendar.php', 'Events Calendar'),
                                                //array('funday.php', 'BSHG Fun Day'),
                                                array('beer-fest.php','Burton in Beer'),
                                                array('raftrace.php', 'Raft Race'),
                                                array('past-events.php', 'Past Events')
                        'interview' => array(
                                    'title' => 'Interviews',
                                    'icon' => 'bubble',
                                    'target' => '#interview',
                                    'sub' => array(
                                                array('john_porteous.php', 'John Porteous'),
                                                array('dave_gibb.php', 'Dave Gibb'),
                                                array('sally_robinson.php', 'Sally Robinson'),
                                                array('edna_lawn.php', 'Edna Lawn')
                        'about' => array(
                                    'title' => 'About Us',
                                    'icon' => 'info',
                                    'target' => '#about',
                                    'sub' => array(
                                                array('about.php', 'About BSHG'),
                                                array('contact.php', 'Contact Us'),
                                                array('ourwork.php', 'Our Work'),
                                                array('media.php', 'In the Media'),
                                                array('links.php', 'Thanks & Links'),
                                                array('sitemap.php', 'Sitemap')
            ) ;

This is the part of the code that writes the main top-level menu, the sub-menus come after. I basically want to find if the page name ($pg) is in the menu section, and apply the currmen id (current menu) to the section where the current page belongs to.

            foreach( $menuarr as $key => $value) {     // Render the main menu
                        if( in_array($pg, $value['sub']) ) { $mid = ' id="currmen"' ; }
                        else { $mid = '' ; }
                        echo '
                                    <a class="sec" href="'.$value['target'].'" title="'.$value['title'].'"'.$mid.'>
                                                <img src="images/'.$value['icon'].'.svg" width="50" height="50" alt="'.$value['title'].'">
                        </li>' ;
            } // end foreach top level

I know that in_array line is wrong, but I’m asking how it should be. The rest of it works fine.

If you change your array a little, this can become much easier.

'interview' => array(
                                    'title' => 'Interviews',
                                    'icon' => 'bubble',
                                    'target' => '#interview',
                                    'sub' => array('john_porteous.php' => 'John Porteous',
                                                , 'dave_gibb.php' => 'Dave Gibb',

Then you can use:
if (isset($value['sub'][$pg]))

To find out if the page exist there.

In the event you need more information for each page, all you need is the identifier to be the same as $pg instead of the default numeric one

1 Like

That did it. Thanks.
Funny, I had the sub-menus like that before, but for some reason changed them to another sub-array. This way is easier to work with.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.