0

I have read several answers, but it is not making sense to me.

Here is the top level array:

$menuData = array( 
    'items' => array(), 
    'parents' => array(),
    'link' => array()
); 

Here is that array when printed out with print_r.

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 4
        )

    [2] => Array
        (
            [0] => 3
        )

    [4] => Array
        (
            [0] => 5
        )

    [5] => Array
        (
            [0] => 6
            [1] => 7
        )

)

I'm trying to figure out how I know when I'm totally done.

I have tried

end($menuData);
$last_id = key($menuData);
echo 'LAST ' . $last_id;

And it echos "link." I understand that. It is the last item. But what if I change that later? I don't want put a test in like if ($last_id == 'link'). That doesn't sound right. I didn't want to do a count++ either because I didn't know how to do it with recursion.

How do I tell, when using recursion, that I am at the end of the outermost array, and that that outermost array's children are also complete? I need to know when the entire array has been processed.

It appears more difficult because it is an associate array.

I have seen this among others:

Get key of the last element in an array

This isn't in the context of **recursion**. array_walk_recursive was not helpful. Didn't see anything here http://php.net/manual/en/ref.array.php.

Wasn't sure of this one, php getting array levels in the recursion

EDIT:

Here is the recursive function:

   // menu builder function, parentId 0 is the root 
   // I just need to know when $menuData is complete done
   // modified from some placy on the Internet I got this from
    function buildMenu($parentId, $menuData, $first=true, $submenu=0) 
    { 

        $html = ''; 

        if (isset($menuData['parents'][$parentId])) 
        { 

            if ($first)
            {
                $html = '<ul id="menu">';
            }


            foreach ($menuData['parents'][$parentId] as $itemId) 
            { 

                $menuId = $menuData['items'][$itemId]['menu_id'];
                $menuName = $menuData['items'][$itemId]['menu_name'];
                $menuDescription = $menuData['items'][$itemId]['menu_description'];
                $menuLink = $menuData['items'][$itemId]['menu_link'];
                $menuColumn = $menuData['items'][$itemId]['menu_column'];
                $menuDropdown = $menuData['items'][$itemId]['menu_dropdown'];
                if ($parentId == 0)
                {

                    $html .= '<li><a href="' . $menuLink . '" class="drop">' . $menuName  . '</a>';
                    $html .= '<div class="dropdown_' . $menuDropdown . 'column"><!-- Begin 1 columns container -->';
                    $html .= '<div class="col_' . $menuColumn . '">';
                    $html .= '<h2>';
                    $html .= ' no parent ';
                    $html .= '</h2></div></div>';

                }
                elseif ($submenu == $parentId)
                { 
                    $html .= '<div class="col_' . $menuData['items'][$itemId]['menu_column'] . '">';
                    $html .= '<h2>' . $menuData['items'][$itemId]['menu_name'] . ' SUB ' . '</h2>'; 
                    $html .= '</div>';
                }
                $previousMenuId = $menuId;

                // find childitems recursively 
                $html .= buildMenu($itemId, $menuData, false, $previousMenuId); 
                $html .= '</li>'; 
            } 
           // $html .= '</ul>'; 
        } 


        return $html; 
    } 
Community
  • 1
  • 1
johnny
  • 19,272
  • 52
  • 157
  • 259

1 Answers1

0

Simple version:

$total = count($array);
foreach($array as $element) {
    $visited++;
    if ($visited == $total) {
       .. at the last element
    }
}
Marc B
  • 356,200
  • 43
  • 426
  • 500