tags:

views:

44

answers:

4

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.

A: 

Submenu only exists in a certain section is the problem.. It exists in the Pricing array and not the Dashboard or Web Site array. one option is to wrap a statement around it to check the section_name, or possibly use a function to check if that array name is defined.

        <ul>
        <?php foreach($menu as $section_name=>$section_array): ?>
            <li><?=anchor($section_array['uri'],$section_name)?>
            <?php
            if ($section_name == 'Pricing')
            {
              if (is_array($section_array['submenu'])) echo 'its an array';
            }
            ?></li>
        <?php endforeach; ?>
        </ul>
davydotcom
+1  A: 

You'll get an error on the first few links (Dashboard and Web Site), but then it should work on pricing because the submenu exists. Try making your if statement a little more complex by checking to make sure the submenu even exists first:

if ($section_array['submenu'] && is_array($section_array['submenu'])){}
Bob Baddeley
I tried that and I'm still getting "Message: Undefined index: submenu" on the other ones.
+3  A: 

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

is_array($section_array['submenu']))

with

array_key_exists('submenu', $section_array) && is_array($section_array['submenu']))

and it should work fine.

Saul
Perfect! I didn't realize I needed to use array_key_exists but it totally makes sense. Thank you!
Perfect Answer Saul.My vote to you.
Nishant Shrivastava
A: 

Normally I don't like mixing PHP and HTML but I think in this case you would profit from a recursive function. Use isset or array_key_exists to see whether a key is in an array:

function createMenuHTML($menu) {
    $html = '<ul>';
    foreach($menu as $section_name=>$section_array) {
        $html .= '<li>' . anchor($section_array['uri'],$section_name);
        if(array_key_exists('submenu', $section_array)) {
            $html .= createMenuHTML($section_array['submenu']);
        }
        $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
}
Felix Kling