-- Waigani <[EMAIL PROTECTED]> wrote
(on Thursday, 11 October 2007, 01:57 AM -0700):
> Here is how I will do it: Generate the array keys as a string and then eval
> the whole thing.

eval() is almost never the right answer; it's both a performance hit and
a security risk.

Variable variables are easy:

    $foo = 'bar';
    $key = 'foo';
    echo $$key;   // 'bar'


> 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.
> 

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to