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!
You should test for whether or not the current item that you’re trying to
echois an array, which can be done withis_array, and then act accordingly. Something like the following might do the trick.