Using a database I store product departments. There are 7 department levels. For the root departments "0" I return a distinct array containing the main departments.

This array will result in 0=>'apparel', 1=>'pet supplies' and so on.
I can therefore get the sub departments of 'apparel' by returning distinct values for level[1] that have level[0] = to 'apparel' and so on. The sub can contain 'accessories' etc. which will have 'jewelry' etc. as its sub down to 7 levels.

Think of it as a google adwords mapping.

What I am trying to do is write a recursive function to iterate through all the root departments on down through the subs and any subs that those subs contain.

To be specific I am trying to generate a store department map for the site dynamically from the department database.

I have not found any solution anywhere and am at a total loss. Anything I have found only takes it down 1 level which isn't very hard to do. I need something that will be recursive through heavily nested results.


For Instance.
the root departments would be similar to:

Array ( [0] => Animals & Pet Supplies [1] => Apparel & Accessories [2] => Arts & Entertainment [3] => Cameras & Optics [4] => Electronics [5] => Furniture [6] => Health & Beauty [7] => Home & Garden [8] => Mature [9] => Media [10] => Office Supplies [11] => Software [12] => Sporting Goods [13] => Toys & Games )

each 1 of those will return an array of there own subs and each one of those another array and so on up to 7 levels of nesting.

A typical sub for array[0] would be..

Array ( [0] => Bird Supplies [1] => Cat Supplies [2] => Dog Supplies [3] => Fish Supplies [4] => Pet Bells & Charms [5] => Pet Bowls, Feeders & Waterers [6] => Pet Carriers & Crates [7] => Pet Collars & Harnesses [8] => Pet Containment Systems [9] => Pet Flea & Tick Control [10] => Pet Food Containers [11] => Pet Grooming Supplies [12] => Pet Leash Extensions [13] => Pet Leashes [14] => Pet Medical Tape & Bandages [15] => Pet Playpens [16] => Pet Steps & Ramps [17] => Pet Strollers [18] => Pet Training Aids [19] => Reptile & Amphibian Supplies [20] => Small Animal Supplies )

and so on.