Traverse Array and Display In Bullet Points

后端 未结 2 882
北荒
北荒 2021-01-17 06:36

I want to traverse this array and display, \'comment\' as bullet points.

Array
(
    [1] => Array
        (
            [id] => 1
            [comment]         


        
相关标签:
2条回答
  • 2021-01-17 07:06

    You need a little bit of recursion

    function traverse_array($array)
    {
        echo '<ul>';
        foreach($array as $element)
        {
            echo '<li>';
            if(isset($element['comment']))
            {
                echo $element['comment'];
            }
            if(is_array($element['children']) && count($element['children']) > 0)
            {
                traverse_array($element['children']);
            }
    
            echo '</li>';
        }
        echo '</ul>';
    }
    
    traverse_array($the_big_array);
    
    0 讨论(0)
  • 2021-01-17 07:16

    Here you go, my hierTree() function by default prints nested ul or ol lists, for an undetermined depth of nested arrays, this function will work out of the box for the example array provided in your question.

    function hierTree($arr, $tag = 'ul', $key = 'comment', $lvl = 0)
    {
        $tabs = (!$lvl)? '': str_repeat("\t", $lvl);
        reset($arr);
        echo "$tabs<$tag>\n";
        while (list(, $v) = each($arr))
        {   
            echo "$tabs\t<li>";
            echo "{$v[$key]}";
            if (count($v['children']))
            {   
                echo "\n";
                hierTree($v['children'], $tag, $key, $lvl +1);
                echo "$tabs\t";
            }   
            echo "</li>\n";
        }   
        echo "$tabs</$tag>\n";
    }
    
    hierTree($tree);
    

    The output of this function will be nicely indented for it to be easily readable.

    Also, If you do hierTree($tree, 'ol'); you will get an ordered list. Id you do hierTree($tree, 'ol', 'id'); You will get an ordered tree and the id field will be print instead of the default comment one.


    Inserting classes.

    If you want to have different classes per list element so you can more easily style on CSS. (although I would recomment to use CSS direct descendant selectors (“>”))

    function hierTree($arr, $tag = 'ul', $key = 'comment', $lvl = 0)
    {
        $tabs = (!$lvl)? '': str_repeat("\t", $lvl);
        reset($arr);
        echo "$tabs<$tag class=\"depth$lvl\">\n"; // ← The change is there.
        while (list(, $v) = each($arr))
        {   
            echo "$tabs\t<li>";
            echo "{$v[$key]}";
            if (count($v['children']))
            {   
                echo "\n";
                hierTree($v['children'], $tag, $key, $lvl +1);
                echo "$tabs\t";
            }   
            echo "</li>\n";
        }   
        echo "$tabs</$tag>\n";
    }
    

    This slightly modified version will print a depthN class per list element. So you can then target their *LI*s by a simple CSS rule such as ul.depth1 > li { ....

    0 讨论(0)
提交回复
热议问题