Here is how I will do it: Generate the array keys as a string and then eval
the whole thing.



Waigani wrote:
> 
> Hi All,
> 
> In the following script, how could I dynamically nest one key inside
> another to generate:
> 
> $menu["tutorials"]["php"]["Language"]="#";
> 
> I'm thinking of something like variable variables, but they don't seem to
> work with array keys - unless I'm doing it wrong.
> 
> here is the script:
> 
> <?php
> 
> /*
> 
>     ---------------------------
>     Author: Vardhan
>     Date: March 03rd, 2005
>     ---------------------------
> …
> */
> 
> $menu=array();
> $menu["home"]="#";
> $menu["tutorials"]=array();
> $menu["tutorials"]["php"]=array();
> $menu["tutorials"]["php"]["Language"]="#";
> $menu["tutorials"]["php"]["Databases"]="#";
> $menu["tutorials"]["php"]["Networking"]="#";
> $menu["tutorials"]["php"]["CLI"]="#";
> $menu["tutorials"]["php"]["Other"]="#";
> $menu["tutorials"]["java"]=array();
> $menu["tutorials"]["java"]["Language"]="#";
> $menu["tutorials"]["java"]["Databases"]="#";
> $menu["tutorials"]["java"]["Networking"]="#";
> $menu["tutorials"]["java"]["CLI"]="#";
> $menu["tutorials"]["java"]["Other"]="#";
> $menu["forums"]="#";
> 
> function display_menu($m) {
>     foreach ($m as $section => $link) {
>         if (!is_array($link))
>             echo "<ul><li> {$link} {$section} </li></ul>";
>         else {
>             echo "<ul><li>{$section}";
>             display_menu($link);
>             echo '</li></ul>';
>         } // end of else
>     } // end of foreach loop
> } // end of function
> 
> display_menu($menu);
> 
> ?>
> 
> Thanks,
> 

-- 
View this message in context: 
http://www.nabble.com/nested-foreach-tf4591373s16154.html#a13151998
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to