Convert array to nested HTML list

Posted by Frank on Stack Overflow See other posts from Stack Overflow or by Frank
Published on 2010-05-08T19:16:11Z Indexed on 2010/05/08 19:18 UTC
Read the original article Hit count: 326

Filed under:
|
|

I have a 2 dimensional array. And each value contains a depth. What I want is that the the array is converted to a (unordered) HTML list. I already found a solution in PHP DOMDocument, but I can't really use it since I have to add a lot of classes and content to the HTML.

I' have tried to make something myself, but it didn't workout.

Here's the array:

array (
  0 => 
  array (
    'name' => 'ELECTRONICS',
    'depth' => '0',
  ),
  1 => 
  array (
    'name' => 'TELEVISIONS',
    'depth' => '1',
  ),
  2 => 
  array (
    'name' => 'TUBE',
    'depth' => '2',
  ),
  3 => 
  array (
    'name' => 'LCD',
    'depth' => '2',
  ),
  4 => 
  array (
    'name' => 'PLASMA',
    'depth' => '2',
  ),
  5 => 
  array (
    'name' => 'PORTABLE ELECTRONICS',
    'depth' => '1',
  ),
  6 => 
  array (
    'name' => 'MP3 PLAYERS',
    'depth' => '2',
  ),
  7 => 
  array (
    'name' => 'FLASH',
    'depth' => '3',
  ),
  8 => 
  array (
    'name' => 'CD PLAYERS',
    'depth' => '2',
  ),
  9 => 
  array (
    'name' => '2 WAY RADIOS',
    'depth' => '2',
  ),
)

The array has to be converted to a list like this:

  • ELECTRONICS
    • TELEVISIONS
      • TUBE
      • LCD
      • PLASMA
    • PORTABLE ELECTRONICS
      • MP3 PLAYERS
        • FLASH
      • CD PLAYERS
      • 2 WAY RADIOS

Thanks in advance,
Frank

© Stack Overflow or respective owner

Related posts about php

Related posts about XHTML