SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Converting a nested array to an HTML nested list?

    How should one convert an array of nested elements into a nested list?

    For instance say you had an array like:
    PHP Code:
    $array = array("Apple"=>array(), "Banana"=>array(), "Tangerine"=>array("Pear"=>array("Walnut"=>array(), "Ice Cream"=>array(), "Candy"=>array()), "Nectar"=>array()), "Honey"=>array(), "Sweets"=>array());

    Array
    (
        [
    Apple] => Array
            (
            )
        [
    Banana] => Array
            (
            )
        [
    Tangerine] => Array
            (
                [
    Pear] => Array
                    (
                        [
    Walnut] => Array
                            (
                            )
                        [
    Ice Cream] => Array
                            (
                            )
                        [
    Candy] => Array
                            (
                            )
                    )
                [
    Nectar] => Array
                    (
                    )
            )
        [
    Honey] => Array
            (
            )
        [
    Sweets] => Array
            (
            )

    And wanted to convert that into a nested list like:
    HTML Code:
    <ul>
    	<li>Apple</li>
    	<li>Banana</li>
    	<li>Tangerine
    		<ul>
    			<li>Pear
    				<ul>
    					<li>Walnut</li>
    					<li>Ice Cream</li>
    					<li>Candy</li>
    				</ul>
    			</li>
    			<li>Nectar</li>
    		</ul>
    	</li>
    	<li>Honey</li>
    	<li>Sweets</li>
    </ul>

  2. #2
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a situation where you would want to use recursion:

    PHP Code:
    <?php
        
    //Make a list from an array
        
    function makeList($array) {

            
    //Base case: an empty array produces no list
            
    if (empty($array)) return '';

            
    //Recursive Step: make a list with child lists
            
    $output '<ul>';
            foreach (
    $array as $key => $subArray) {
                
    $output .= '<li>' $key makeList($subArray) . '</li>';
            }
            
    $output .= '</ul>';
            
            return 
    $output;
        }

        
    $array = array("Apple"=>array(), "Banana"=>array(), "Tangerine"=>array("Pear"=>array("Walnut"=>array(), "Ice Cream"=>array(), "Candy"=>array()), "Nectar"=>array()), "Honey"=>array(), "Sweets"=>array());
        echo 
    makeList($array);
    ?>
    Outputs:

    Code HTML:
    <ul><li>Apple</li><li>Banana</li><li>Tangerine<ul><li>Pear<ul><li>Walnut</li><li>Ice Cream</li><li>Candy</li></ul></li><li>Nectar</li></ul></li><li>Honey</li><li>Sweets</li></ul>

    Displays as:

    • Apple
    • Banana
    • Tangerine
      • Pear
        • Walnut
        • Ice Cream
        • Candy
      • Nectar
    • Honey
    • Sweets

  3. #3
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much

    I made some modifications that alter the formatting of the output with added whitespace, and also allows you to optionally pass in an array containing alternate keys (so you can for instance replace numeric ID's with names):
    PHP Code:
    function makeList($array$depth=0$key_map=FALSE) {
        
    $whitespace str_repeat("\t"$depth*2);
        
    //Base case: an empty array produces no list
        
    if (empty($array)) return '';
        
    //Recursive Step: make a list with child lists
        
    $output "$whitespace<ul>\n";
        foreach (
    $array as $key => $subArray) {
            
    $subList makeList($subArray$depth+1$key_map);
            if(
    $key_map AND $key_map[$key]) $key $key_map[$key];
            if(
    $subList$output .= "$whitespace\t<li>" $key "\n" $subList "$whitespace\t</li>\n";
            else 
    $output .= "$whitespace\t<li>" $key $subList "</li>\n";
        }
        
    $output .= "$whitespace</ul>\n";
        return 
    $output;


  4. #4
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about if you had to create nested list from such array:
    PHP Code:
    'categories' => array(
    '1' => array('id' => 1,    'parentid' => 0'title' => 'Category 1''url' => 'url-1'),
    '2' => array('id' => 2'parentid' => 0'title' => 'Category 2''url' => 'url-2'),

    '3' => array('id' => 3,    'parentid' => 1'title' => 'Category 3''url' => 'url-3'),
    '4' => array('id' => 4'parentid' => 1'title' => 'Category 4''url' => 'url-4'),

    '5' => array('id' => 5,    'parentid' => 3'title' => 'Category 5''url' => 'url-5'),
    '6' => array('id' => 6'parentid' => 3'title' => 'Category 6''url' => 'url-6'),
    ); 
    And the result ul list to contain list's with the link:

    <li><a href="url"> title</a></li>

    Novice here, please help

  5. #5
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'd have to convert that array to the format the makeList() function accepts. Make it a nested array, with the $key_map array containing the anchor text. e.g. $key_map = array('1'=>'<a href="url-1">Category 1</a>', ...)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •