|  | Posted by Ewoud Dronkert on 10/14/05 13:29 
Oliver Saunders wrote:> What kind of loop do you need to turn an array like this:
 >
 > $notes = array(
 >   '1:1',
 >   '1:2',
 >   '1:3',
 >   array(
 >     '2:1',
 >     '2:2',
 >     array(
 >       '3:1',
 >       '3:2'
 >     ),
 >     '2:3'
 >   ),
 >   '1:4',
 >   array(
 >     '2:4',
 >     '2:5'
 >   )
 > );
 >
 > into a list like this:
 >
 > <ul>
 >   <li>1:1</li>
 >   <li>1:2</li>
 >   <li>1:3</li>
 >   <ul>
 >     <li>2:1</li>
 >     <li>2:2</li>
 >     <ul>
 >       <li>3:1</li>
 >       <li>3:2</li>
 >     </ul>
 >     <li>2:3</li>
 >   </ul>
 >   <li>1:4</li>
 >   <ul>
 >    <li>2:4</li>
 >    <li>2:5</li>
 >   </ul>
 > </ul>
 
 Like Erwin noticed, the code wasn't correct. I've updated it re. commas
 and semicolons.
 
 function makelist($a)
 {
 static $level = -1;
 
 ++$level;
 $spc = str_repeat(' ', 2 * $level);
 $html = "$spc<ul>\n";
 foreach ( $a as $i )
 {
 if ( is_array($i) )
 $html .= makelist($i);
 else
 $html .= "$spc  <li>$i</li>\n";
 }
 $html .= "$spc</ul>\n";
 
 --$level;
 return $html;
 }
 
 echo makelist($notes));
 
 --
 E. Dronkert
 [Back to original message] |