recursion - Recursively creating a multi-dimensional array in PHP -


I am trying to find out the best way to write a PHP function, which is a multi-dimensional array with an unknown The sub-level number from a mysql table will be re-purposed to create a data structure that can be stopped to create a navigation menu on a website, with each menu item there is a submenu with hair menu items. .

Field of Notes in the table:
EITIIDIT
ITIPITID
VirtualMatTest
Text ItemLink • TNIINS KDrl

The function will be an example of an array returned : $ MenuItems = array (itemID # => array ('ItemText' => 'Home', 'ItemLink' => 'index.php', 'Children' => Array (itemID # => Array ('ItemText' => 'Home all 1', 'item linked' => 'some pages .php', 'child' => 0), item id = = array ('item' text '=' 'home all 2 ',' Maddink '=>' somepage2.php ',' children '=> 0),)), item id => array (' item text '=>' contact ',' aa Tmalink '=>' contact.php ',' baby '=> 0)));

Most appreciate if someone can tell me in the right direction to complete this. Thanks!

It is not very difficult to do what you do in the menu items stored in an array, Can see them from. Then you are repeated on the menu item, and if they have a non-ParentID, then you add them to the list of your parents' children. Then you remove all the children from the master list so that you will only have top-level items left.

Code:

  & lt ;? Php $ menuItems = array (1 = & gt; array ('item' text => 'home', 'item link' = & gt; 'index.php', 'parentID' =>, tap), 2 = & Gt; array ('item text' = & gt; 'Home sub 1', 'Madelink' => gt; somepage.php ',' ParentID '=> 1,), 3 = & gt; array ('Item text' = & gt; 'Home sub 2', 'Madelink' => gt; Somepage2.php ',' ParentID '=>, 4 = & gt; array (' item text ' = & Gt; 'Contact', 'ItemLink' = & gt; 'contact.php', 'ParentID' => Zero;);); // Each node starts with 0 children foreach ($ menuItems & amp; $ menuItem) $ menuItem ['children'] = array; // If there is ParentID in the menu item, then add it as parent child array foreach ($ menuItems $ id = & gt; & amp; $ menuItem) {$ menuItem ['ParentID'] = zero) $ menuItems [$ MenuItem ['ParentID']] ['child'] [$ id] = & amp; $ MenuItem; } // Remove the children from the $ $ item. Items are therefore only top-level items foreach (live as array_keys ($ menuItems) $ id) {if ($ menuItems [$ ID] ['ParentID']! = Null) Unset ($ menuItems [$ ID]); } Print_r ($ menuItems); ? & Gt; Aare ([1] = & gt; array ([madmext] => home [itemink] = & index; php [/ code>  

output:

 < Code> ParentID] => [Child] => Array ([2] => Array ([Madmext] => Home all 1 [Item Linked] => Some Pages .php [ParentID ] = & Gt; 1 [child] = & gt; array ()) [3] = & gt; Array ([item text] => home all 2 [item link] => some page 2. php [ParentID ] = & Gt; 1 [Child] => array ())) [4] => Array ([ItemText] => Contact [item link] => contact.php [ParentID] = & Gt; [child] => array ())  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -