Hi guys,
I am trying to figure out the best way to write a PHP function that will recursively build a multi-dimensional array with an unknown number of sublevels from a mysql table. Its purpose is to create a data structure which can be looped through to create a navigation menu on a website, with each menu item possibly having a submenu with child menu items.
The fields of note in the table are:
int ItemID
int ParentID
varchar ItemText
text ItemLink
tinyint HasChildren
So an example of a returned array from the function would be:
$menuItems = array( itemID# => array( 'ItemText' => 'Home', 'ItemLink' => 'index.php', 'Children' => array( itemID# => array ( 'ItemText' => 'Home Sub 1', 'ItemLink' => 'somepage.php', 'Children' => 0 ), itemID# => array ( 'ItemText' => 'Home Sub 2', 'ItemLink' => 'somepage2.php', 'Children' => 0 ), ) ), itemID# => array( 'ItemText' => 'Contact', 'ItemLink' => 'contact.php', 'Children' => 0 ) ) );
Would greatly appreciate if someone could point me in the right direction to accomplish this. Thanks!