I spent some time on solving this issue but I can't seem to get to where I need to be. I have navigation in a MySQL database like so.
Basically, the Parent_ID field lists the ID of the item just above each one in the hierarchy. If the Parent_ID is 0, it is at the top level. The Order number is the order that each item is in within that level. This is pretty standard stuff.
I want to put the entire thing into an array. The final array would look like this.
I want to do this recursively so I don't have to limit the number of levels. How would I build this array? I've tried a few things but nothing's working right.
Thanks!
Code:
ID Name Parent_ID Order
----------------------------------
1 Category 1 0 1
2 Category 2 0 2
3 Product 1 1 1
4 Product 2 1 2
5 Attribute 1 3 2
6 Attribute 2 3 1
I want to put the entire thing into an array. The final array would look like this.
Code:
(
[1] => Array
(
[id] => 1
[name] => Category 1
[1] => Array
(
[id] => 3
[name] => Product 1
[1] => Array
(
[id] => 6
[name] => Attribute 2
)
[2] => Array
(
[id] => 5
[name] => Attribute 1
)
)
[2] => Array
(
[id] => 4
[name] => Product 2
)
)
[2] => Array
(
[id] => 2
[name] => Category 2
)
)
Thanks!