Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

array of arrays to list of lists

0 views
Skip to first unread message

jeff

unread,
Dec 27, 2009, 1:16:26 AM12/27/09
to
Lets say we have an array of arrays:

$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

rf

unread,
Dec 27, 2009, 2:11:39 AM12/27/09
to

"jeff" <jeff_...@att.net> wrote in message
news:hh6u3r$bbe$1...@news.albasani.net...

> Lets say we have an array of arrays:
>
> $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.

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.


0 new messages