Jump to content

Help Building menu from arrays


dukeraoul

Recommended Posts

I have a multidimensional array full of arrays like this:

 

The arrays represent hierarchy's and can be as deep as necessary.

 

Array ( [0] => Outdoors [1] => Regions [2] => Southern Rivers )

 

Array ( [0] => Outdoors [1] => Activity [2] => Scenic Viewing )

 

Array ( [0] => Outdoors [1] => Activity [2] => Fishing )

 

Array ( [0] => Outdoors [1] => Activity [2] => Camping )

 

Array ( [0] => Outdoors [1] => Activity [2] => Horseback Riding )

 

Array ( [0] => Outdoors [1] => Regions [2] => Mountains )

 

Array ( [0] => Outdoors [1] => Activity [2] => Fishing )

 

That I want to sort into a single multidimensional array which maintains parent child relations... so some thing like this:

 

Array (

  [Outdoors] => Array (

          [Activity] => Array (

                    [0] => Fishing, [1] => Camping, [1] => Scenic Viewing)), 

          [Regions] => Array (

                    [0] => Mountains, [1] => Southern Rivers )),

)

 

Except that it can expand as deep as the hierarchy demands.

 

I believe that is the result I want although in the end I want to just list the info in the correct hierarchy.

 

I have been stuck on this problem for a while now and any help or insight would be appreciated.

 

Thanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/72648-help-building-menu-from-arrays/
Share on other sites

Actually I was looking more for advice on the angle I should attack this problem.

 

I have tried parsing and sorting the data a multiple amount of ways and I keep getting hung up.

 

I guess I should have been more specific.

 

Vague psuedocode would be preferable... but any hint in the right direction would be great.

 

If you want my latest attempt at parsing it I can postthe code, but I am sure its all pretty redundant.

In this case, i personaly favour an approach where you have two levels for each item in your array: a parent and a child. I think this makes it easier to deal with than the case where you have nested parent child relationships within your array. This is probably easier to explain by example. I wrote this a while back for another post:

 

<?php

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

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

 

Given your current array structure, it would be relatively easy to transform that array to work with the above function. Something like:

<?php
$array = array(array('Outdoors','Regions','Southern Rivers'),array('Outdoors','Activity','Scenic Viewing'));
function createarray($array){
foreach($array as $k => $v){
	foreach($v as $k2 => $v2){
		if($k2 == 0){
			$parent = 'Parent';
		}else{
			$parent = $array[$k][$k2-1];
		}
		$new_array[$parent][$v2] = $v2;
	}
}
return $new_array;
}
?>

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.