views:

46

answers:

2

Hey all,

This should be very basic, but I am a little stumped!

Here is my array:

$menu = array(
  'Home',

  'Stuff'=>array(
    'Losta Stuff',
    'Less Stuff',
    'Ur moms stuff',
    'FAQ'
 ),
  'Public Works'

);

Here is my logic:

echo "<ol>\n";
foreach( (array)$menu as $header )
{
  echo '  <li><b>'.$header."</b><br />\n";
 echo '  <ol>';
  foreach( (array)$header as $headers )
  {
    echo '    <li>'.$headers.".</li>\n";
  }
  echo '  </ol>';
}
echo "</ol>\n";

As you can see, Home and Public Works don't have data in the them, so I get a

Warning: Invalid argument supplied for foreach() in test.php on line ##

If I add (array) to $header like this: foreach( (array)$header as $headers ), It no longer gives me the error, but it just displays the $header as the $headers (i.e. Home - Home, Instead of Home - nothing).

Basically, if the data is empty, I want it to do nothing!

+2  A: 

I see something like this:

// your old menu was using keys for headers on "submenus" only
// this one uses keys for headers for everything
$menu = array(
  'Home'=>'Home',   
  'Stuff'=>array(
    'Losta Stuff',
    'Less Stuff',
    'Ur moms stuff',
    'FAQ'
  ),
  'Public Works' => 'Public Works',    
);
echo "<ol>\n";
foreach( (array)$menu as $header => $items )
{
  echo '  <li><b>'.$header."</b>";
  if (is_array($items)) {
    echo "<br />\n";
    echo '  <ol>';
    foreach( $items as $subhead )
    {
      echo '    <li>'.$subhead.".</li>\n";
    }
    echo '  </ol>';
  }
}
echo "</ol>\n";

Using is_array to determine if there are more options underneath the current menu.

gnarf
That was the trick thanks!
Jared
$header probably shouldn't be echoed if it was an array
AlReece45
@AlReece45 saw that after the fact ;) - Modified the original array slightly - icio's solution seems cleaner anyway with the original array.
gnarf
+2  A: 

You should test for whether or not the current item that you're trying to echo is an array, which can be done with is_array, and then act accordingly. Something like the following might do the trick.

<?php 

$menu = array(
    'Home',
    'Stuff'=>array(
        'Losta Stuff',
        'Less Stuff',
        'Ur moms stuff',
        'FAQ'
    ),
    'Public Works'
);

echo "<ol>\n";
foreach($menu as $menuName => $header )
{
    if (!is_array($header))
    {
        echo '  <li><b>'.$header."</b><br />\n";
    }
    else
    {
        echo "<li><b>$menuName</b><ol>";
        foreach($header as $headers )
        {
            echo '    <li>'.$headers.".</li>\n";
        }
        echo "</ol></li>";
    }
}
echo "</ol>\n";
icio
Thanks - I noticed the problem with the original response just now. Question though - when I wrap this in a function (i.e. `function menu()`), why does it give me errors?
Jared
It's hard to say without knowing what error you are receiving. I'm *guessing* it's because you have `$menu = array(...);` still outside of the `menu()` function. If that's the case put `global $menu;` in the first line of your function so that PHP knows where to find the array.
icio
yes that was it.. thanks!
Jared