views:

262

answers:

2

I'll be the first to admit that PHP is not my forte and this array is starting to drive me nuts.

This is the array I'm attempting to enumerate:

// array containing the site menu
$sitemap = array(
    array( 'Title' => 'menu01',
        'Description' => 'menu01_description', 
        'Address' => 'http://localhost/site.php?page=menu01',
        ),
    array( 'Title' => 'menu02',
        'Description' => 'menu02_description', 
        'Address' => 'http://localhost/site.php?page=menu02',
        ),
    array( 'Title' => 'menu03',
        'Description' => 'menu03_description', 
        'Address' => 'http://localhost/site.php?page=menu03',
        )
);

Right now I'm operating under the assumption that I need to use a foreach loop to enumerate the contents of my array.

This is the function I'm currently trying to implement, but it keeps returning an error stating that my arguments are not valid.

function GenerateMenu(){
    $output = "<ul>";

    foreach ($sitemap as $menuitem => $value){
        if ($page == $value["Title"]){
            $output .= '<li class="active">';
        }
        else {
            $output .= '<li>';
        }

        $output .= '<a href="' . $value['Address'] . '">' . $value['Description'] . '</a><li>';
    }

    $output .= "</ul>";
    return $output;
}

Why are my arguments invalid? What best way to print my array?

+3  A: 

You forgot to specify/pass $page and $sitemap. Where do that variables come from?

Try pass it to your function:

function GenerateMenu($sitemap, $page) {
    // …
}

Then you can call it like this:

$sitemap = array(
    // …
);
$page = 'menu01'
echo GenerateMenu($sitemap, $page);
Gumbo
A: 

The function GenerateMenu() needs to be sent the value for $page and $sitemap or it doesn't know what to do.

Amanda