Jump to content

Best way to iterate through a mulitdimensional array?


gwolgamott

Recommended Posts

I'm again dealing with arrays, i think the people are trying to torture me with this. But dealing with someone elses array now.... so I get this from their code:

Array (
[Events] => Array ( [New Folder] => Array ( [0] => LINK_ME ) )
[Help] => Array ( [0] => LINK_ME ) 
[Home] => Array ( [0] => LINK_ME ) 
[Management System Documentation] => Array ( [All Forms] => Array ( [0] => LINK_ME ) [Engineering] => Array ( [0] => LINK_ME ) [Lateral] => Array ( [0] => LINK_ME ) [Pedestal] => Array ( [0] => LINK_ME ) [Random] => Array ( [0] => LINK_ME ) [shipping & Recieving] => Array ( [Recieving] => Array ( [0] => LINK_ME ) [shipping] => Array ( [0] => LINK_ME ) ) ) 
[Newsletter] => Array ( [0] => LINK_ME ) )

 

Now I need to iterate through it. doing it once to create a link for a php/js dynamic menu system. It uses a table listing to create submenus that are hidden to create the menu. Well here the code to get the first level and whether accourding to the above array whether it has a submenu (as in checking if it's value is an array actually) anyways not my problem that works:

 

//code for first level only of navigation
//see include for submenu lists
foreach ($indexed_array as $key => $value)
{
if($key != "Home") // we always want home to be first link, taken care of prior to here
	{
	echo('<li><a href="');  //start first section of link for
	if($value[0] == "LINK_ME")  // if the array value is equal to call variable print direct link
		{
		echo('index.php?page=').$key;
		}
	else   //else it is not a direct link, and is thus value is an array print link with reference to submenus
		{
		$sub_menu++; //reference to what submenu we are on
		echo('" rel="ddsubmenu').$sub_menu;
		//" rel="ddsubmenu
		}	
	echo ('">');
	echo $key."</a></li>";
	}
}

But my problem is applying this method to create something like below from the array using that submenu format... I've a few ideas but seem to get lost trying to them... ideas?

 

<!--HTML for the Drop Down Menus associated with Top Menu Bar-->
<!--They should be inserted OUTSIDE any element other than the BODY tag itself-->
<!--A good location would be the end of the page (right above "</BODY>")-->
<!--Top Drop Down Menu 1 HTML-->
<ul id="ddsubmenu1" class="ddsubmenustyle">
<li><a href="#">Item 1a</a></li>
<li><a href="#">Item 2a</a></li>
<li><a href="#">Item Folder 3a</a>
  <ul>
  <li><a href="#">Sub Item 3.1a</a></li>
  <li><a href="#">Sub Item 3.2a</a></li>
  <li><a href="#">Sub Item 3.3a</a></li>
  <li><a href="#">Sub Item 3.4a</a></li>
  </ul>
</li>
<li><a href="#">Item 4a</a></li>
<li><a href="#">Item Folder 5a</a>
  <ul>
  <li><a href="#">Sub Item 5.1a</a></li>
  <li><a href="#">Item Folder 5.2a</a>
    <ul>
    <li><a href="#">Sub Item 5.2.1a</a></li>
    <li><a href="#">Sub Item 5.2.2a</a></li>
    <li><a href="#">Sub Item 5.2.3a</a></li>
    <li><a href="#">Sub Item 5.2.4a</a></li>
    </ul>
  </li>
</ul>
</li>
<li><a href="#">Item 6a</a></li>
</ul>

  Best way to iterate through a multi dimensional array is to create a function and have that function return the pieces you want formatted the way you want, inside the function will be the foreach loop extracting this information. But on the value for each piece returned it will first check if the value is an array "is_array()" if the value is an array, then call the function and pass it value. Yes call the same function from with in it's self and pass the value you want to break down. Then the function will break down to the lowest level before processing and then backing back out to the top array. Let me know if you need help beyond the explanation.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.