Jump to content

[SOLVED] Multidimensional Array - Breadcrumb Menu


tarun

Recommended Posts

<?php
$menu["Home"] = "/index.php";
     $submenu["Home"]["About"] = "/about.php";

$menu["Contact Us"] = "/contactus.php";
     $submenu["Contact Us"]["Live Chat"] = "/chat/index.php";
          $submenu["Contact Us"]["Live Chat"]["Live Help"] = "/chat/connected.php";
     $submenu["Contact Us"]["Email"] = "/emailform.php";

$menu["Members"] = "/members/index.php";
     $submenu["Members"]["Profile"] = "/members/profiles/index.php";
          $submenu["Members"]["Profile"]["View Profile"] = "/members/profiles/viewprofile.php";
          $submenu["Members"]["Profile"]["View All Profiles"] = "/members/profiles/viewall.php";
          $submenu["Members"]["Profile"]["Edit Profile"] = "/members/profiles/edit.php";
     $submenu["Members"]["Check Messages"] = "/members/mail.php";
     $submenu["Members"]["Send A Message"] = "/members/send.php";
     $submenu["Members"]["Edit Profile"] = "/members/editprofile.php";

?>

Well... Thats My Multi Dimensional Array

How Would I Go About Making A Breadcrumb Menu From It

 

In Case You Didn't No A Breadcrumb Menus Are Like This:

[iurl=#]Members[/iurl] / [iurl=#]Profile[/iurl] / [iurl=#]Edit Profile[/iurl]

Well, with the array set up as you have it, you'll run into problems. You can't have an element of an array as BOTH a string and an array. For instance, you have

$submenu["Contact Us"]["Live Chat"] equal to the string "/chat/index.php" and it is also an array.

 

If you were to modify your $submenu array so that it only ever has two dimensions, the first being the parent node, the second the child, you can write a recursive function similar to:

 

<?php

function menu($menu,$index='Parent'){
foreach($menu[$index] as $k => $v){
	echo "<li><a href='$v'>$k</a></li>";
		if(is_array($menu[$k])){
		echo '<ul>';
		menu($menu,$k);
		echo '</ul>';
	}
}	
}
$menu = array();
$menu["Parent"]["Home"] = "/index.php";
$menu["Parent"]["Contact Us"] = "/contactus.php";
$menu["Parent"]["Members"] = "/members/index.php";
$menu["Home"]["About"]  = "/about.php";
$menu["Contact Us"]["Live Chat"] = "/chat/index.php";
$menu["Live Chat"]["Live Help"] = "/chat/connected.php";
$menu["Contact Us"]["Email"] = "/emailform.php";

echo '<ul>';
menu($menu);
echo '</ul>';
?>

 

Give that a shot.

 

P.S. 1000th post :P

 

EDIT: Reposted with a neater function.

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.