Rendering nested array in a  with headings I have an array with the following structure

Array 
( 
    [0] =&gt; Array 
        ( 
            [area] =&gt; Area ABC 
            [activity] =&gt; Array 
                ( 
                    [0] =&gt; Activity 1
                    [1] =&gt; Activity 2
                ) 
        ) 
) 


I'm trying to render this array in the following way

Area ABC


Activity 1 
Activity 2


And am currently using the following code to achieve this

$status = 'closed';
foreach($suggestions as $suggestion)
    {
    if($currentarea != $suggestion['area'])
        {
        echo "&lt;h3&gt;";
        echo $suggestion['area'];
        echo "&lt;/h3&gt;";
        if($status == 'closed')
            {
            echo "&lt;ol&gt;";
            $status = 'open';
            }
        }
    foreach($suggestion['activity'] as $activity)
        {
            echo "&lt;li&gt;";
            echo $activity;
            echo "&lt;/li&gt;";
        }
    if($status == 'open')
        {
            echo "&lt;/ol&gt;";
            $status = 'closed';
        }
    $currentarea = $suggestion['area'];
    }


Is this the best way of doing it, or is there a better way, I will have more areas and activities, but the structure stays the same.
This won't work if each suggestion does not start a new area.  


foreach($suggestions as $suggestion)
    {
    if($currentarea != $suggestion['area'])
        {
        echo "&lt;h3&gt;";
        echo $suggestion['area'];
        echo "&lt;/h3&gt;";
        if($status == 'closed')
            {
            echo "&lt;ol&gt;";
            $status = 'open';
            }
        }
    foreach($suggestion['activity'] as $activity)
        {
            echo "&lt;li&gt;";
            echo $activity;
            echo "&lt;/li&gt;";
        }
    if($status == 'open')
            {
            echo "&lt;/ol&gt;";
            $status = 'closed';
            }
    $currentarea = $suggestion['area'];
    }



You close on every iteration that it's open and add list items on every iteration, but you only open if the area changes.  

foreach($suggestions as $suggestion)
{
    echo '&lt;h3&gt;';
    echo $suggestion['area'];
    echo "&lt;/h3&gt;\n";
    echo "&lt;ol&gt;\n";

    foreach($suggestion['activity'] as $activity)
    {
        echo '  &lt;li&gt;';
        echo $activity;
        echo "&lt;/li&gt;\n";
    }

    echo "&lt;/ol&gt;\n";
}


This code is simpler and will work more reliably.  

This is the wrong place to discuss how to write new code.  But if you want to get the previous concept working, you'll want to open and close inside the if that checks if the area has changed.  Or pull off the first element of the list so that you can treat it differently.  Or make a new list where there's only one suggestion per area, combining multiple suggestions with the same area into one.