I've got a database holding web pages and their parent/child relationships. I made a single query using self joins that returns an array with up to 10 levels deep (not that I'll ever use it, but still). Then I unset all the empty levels, so i have an array (at the moment) that is only three levels deep. What I'd like to do is create a second array out of this array that holds a navigational tree where children are arrays of their parents and both the key and value of the tree array is set to the id in the database.
here's what my array looks like using print_r
Array
(
[0] => Array
(
[lev1] => index
)
[1] => Array
(
[lev1] => company
[lev2] => careers
)
[2] => Array
(
[lev1] => company
[lev2] => contact
)
[3] => Array
(
[lev1] => solutions
[lev2] => information-technology
[lev3] => business-processes
)
[4] => Array
(
[lev1] => solutions
[lev2] => information-technology
[lev3] => network-strategy
)
[5] => Array
(
[lev1] => solutions
[lev2] => information-technology
[lev3] => virtual-private-networks-vpns
)
[6] => Array
(
[lev1] => solutions
[lev2] => information-technology
[lev3] => network-security
)
[7] => Array
(
[lev1] => solutions
[lev2] => information-technology
[lev3] => disaster-recovery
)
[8] => Array
(
[lev1] => solutions
[lev2] => site-development
[lev3] => internet-strategies
)
[9] => Array
(
[lev1] => solutions
[lev2] => site-development
[lev3] => web-sites
)
[10] => Array
(
[lev1] => solutions
[lev2] => site-development
[lev3] => intranets
)
[11] => Array
(
[lev1] => solutions
[lev2] => site-development
[lev3] => content-management
)
[12] => Array
(
[lev1] => process
)
[13] => Array
(
[lev1] => case-studies
)
[14] => Array
(
[lev1] => get-started
)
[15] => Array
(
[lev1] => index4
)
)
The problem is that lev1 can be repeated if it has children, and if lev1 does have children, it's included in the array with it's children.
So for example, company isn't included as [lev1] in it's own array, it's shown as
[lev1] => company
[lev2] => careers
next...
[lev1] => company
[lev2] => contact
So the structure is company > careers and company > contact
similary
solutions
information technology
business-processes
represents solutions > information technology > business processes, but this is the first time it defines any of those three, so the two parents were not defined earlier.
It seemed simple enough when I started, but I'm struglling writing a loop that will go down to 10 levels deep and build me a navigational tree from this array with unique levels.
Any help is appreciated.