I have this array that I am having trouble traversing:
print_r($menu) gives this:
[Dashboard] => Array
(
[uri] => dashboard
[access_level] => Full
)
[Web Site] => Array
(
[uri] => website
[access_level] => Full
)
[Pricing] => Array
(
[uri] => pricing
[access_level] => Full
[submenu] => Array
(
[Change Pricing] => Array
(
[uri] => pricing/change
[access_level_required] => Full
)
)
)
I am trying to access each of the main areas using a foreach loop. That part works fine, but I’m having problems accessing the “submenu” array in the pricing array.
<ul>
<?php foreach($menu as $section_name=>$section_array): ?>
<li><?=anchor($section_array['uri'],$section_name)?>
<?php
if (is_array($section_array['submenu'])) echo 'its an array';
?></li>
<?php endforeach; ?>
</ul>
Right now I can’t even tell if $section_array[‘submenu’] is an array. I must be accessing it incorrectly, but I’m not sure why. I am getting error: “Message: Undefined index: submenu”
Btw this is written with CodeIgniter 1.7.2 short tags, but you should be able to catch the drift of my foreach.
The message is there because in Your example only one of the first level elements has the ‘submenu’ key defined. Namely, the last one. For all others that key is missing.
Replace
with
and it should work fine.