Associate
Hi folks,
I think this is an easy one, but its the first time I have attempted recursion.
I have a MySQL table that stores items for a menu. It has the following fields:
- id
- depth
- left_id
- right_id
- title
So I have entered the data (manually at the moment, but will be automated eventually), and they look like this when selected into an array:
This should produce a menu structure of:
Can anyone create a simple recursive function to generate that structure from the above array?
Cheers,
Matt
I think this is an easy one, but its the first time I have attempted recursion.
I have a MySQL table that stores items for a menu. It has the following fields:
- id
- depth
- left_id
- right_id
- title
So I have entered the data (manually at the moment, but will be automated eventually), and they look like this when selected into an array:
Code:
Array
(
[0] => Array
(
[id] => 1
[depth] => 0
[left_id] => 0
[right_id] => 2
[title] => Home
)
[1] => Array
(
[id] => 2
[depth] => 0
[left_id] => 1
[right_id] => 4
[title] => News
)
[2] => Array
(
[id] => 4
[depth] => 1
[left_id] => 2
[right_id] => 5
[title] => Archive
)
[3] => Array
(
[id] => 5
[depth] => 2
[left_id] => 4
[right_id] => 3
[title] => Really old stuff
)
[4] => Array
(
[id] => 3
[depth] => 0
[left_id] => 5
[right_id] => 0
[title] => Contact us
)
)
This should produce a menu structure of:
Code:
- Home
- News
-- Archive
--- Really old stuff
- Contact us
Can anyone create a simple recursive function to generate that structure from the above array?
Cheers,
Matt