Jump to content

multidimensional array with various levels of nested arrays


uneekvision

Recommended Posts

I'm a little stuck on how to properly build a multidimensional with various levels from a database.

 

This is array is the result of a database query:

Array
(
    [0] => 1.0
    [1] => 2.0
    [2] => 2.1.0
    [3] => 2.2.0
    [4] => 2.2.1.0
    [5] => 2.2.2.0
    [6] => 2.2.3.0
    [7] => 2.2.3.1.0
    [8] => 3.0
    [9] => 3.1.0
    [10] => 3.2.0
    [11] => 4.0
    [12] => 5.0
    [13] => 6.0
    [14] => 6.1.0
    [15] => 6.2.0
)

Array
(
    [0] => home
    [1] => company
    [2] => about-us
    [3] => contact-us
    [4] => form
    [5] => directions
    [6] => directory
    [7] => dial-by-name
    [8] => portfolio
    [9] => projects
    [10] => testimonials
    [11] => locations
    [12] => products
    [13] => services
    [14] => b2b
    [15] => consumer
)

To explain how the two arrays are associated...

The first number in the series defines the parent level. The second number defines the child of the previous number and so on down the line until 0 is reached.

Example:

 

$item[7] = 2.2.3.1.0
//needs to translates to
      2      .      2       .      3      .       1       .       0
$item["Company"]["Contact Us"]["Directory"]["Dial By Name"] = "dial-by-name";

 

The beginning of the nested array is Company, the second level in Company is Contact Us which is the second item in this array and holds a new array called Directory. Directory's first item is Dial By Name which does not have an array as a value, so the value is itself. 

 

Below is what I need to turn the above into...

$menu = 
array( 
"Home" => "home",
  	"Company" => 
  	array(
	"About Us" => "about-us",
	"Contact Us" => 
	array(
		"Form" => "form",
		"Directions" => "directions",
		"Directory" =>
		array(
			"Dial By Name" => "dial-by-name"
			)
		)
	),
"Portfolio" =>
array(
	"Projects" => "projects",
	"Testimonials" => "testimonials"
	),
"Locations" => "locations",
"Products" => "products",
"Services" =>
array(
	"B2B" => "b2b",
	"Consumer" => "consumer"
	)
);

 

As far as formating the text that goes into the key I can handle. The part I'm stuck on is building the loop that will create the array then each new instance of an array when the levels are called for.

 

I hope this makes sense to someone. If anyone has suggestions of other ways that I can accomplish this I'm open to anything.

 

My goal is to create a tree menu stored in mysql that has the ability to have unlimited nested arrays (not that you would need it but still that is my goal)

 

Thanks in advance for any guidance.

 

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.