$A['first'] = array('one','two','three');
$A['second'] = array('1','2');
and we want to turn this into a list of lists:
<ul>
<li>first
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</li>
<li>second...
This is a fairly common thing to do, say with site maps of pages in
sections, or lists of products in categories.
It seems to me that there must be a straight forward way of making
these such that you could set up a function and feed it the params and
details of what your lists will look like (usually links, and rows of
data from a database) but I can't quite wrap my mind around a clean and
simple way of doing this. I get hung up in the recursive bit.
Who has a solution or hint for this common chore? Or is this a messy
little class?
Jeff
Yes. I do this all the time.
> It seems to me that there must be a straight forward way of making these
> such that you could set up a function and feed it the params and details
> of what your lists will look like (usually links, and rows of data from a
> database) but I can't quite wrap my mind around a clean and simple way of
> doing this. I get hung up in the recursive bit.
There is no recursion involved.
> Who has a solution or hint for this common chore? Or is this a messy
> little class?
For a start:
foreach($A as $k => $b)
{
// do stuff with $k
foreach ($b as $c)
{
//do stuff with $c
}
// do stuff
}
Then wrap it up in a class if you wish, bot not a messy one please but one
tailored to your specific requirements.
I'm sure (without even looking) that there is something in the namual
covering this.