Php 遍历数组并以项目符号显示

Php 遍历数组并以项目符号显示,php,multidimensional-array,Php,Multidimensional Array,我想遍历这个数组并将“comment”显示为项目符号 Array ( [1] => Array ( [id] => 1 [comment] => a [parent_id] => 0 [children] => Array ( [3] => Array

我想遍历这个数组并将“comment”显示为项目符号

Array
(
    [1] => Array
        (
            [id] => 1
            [comment] => a
            [parent_id] => 0
            [children] => Array
                (
                    [3] => Array
                        (
                            [id] => 3
                            [comment] => c
                            [parent_id] => 1
                            [depth] => 0
                            [child_count] => 0
                            [children] => 
                        )

                    [4] => Array
                        (
                            [id] => 4
                            [comment] => d
                            [parent_id] => 1
                            [depth] => 0
                            [child_count] => 0
                            [children] => 
                        )

                )

            [depth] => 1
            [child_count] => 2
        )

    [2] => Array
        (
            [id] => 2
            [comment] => b
            [parent_id] => 0
            [children] => Array
                (
                    [5] => Array
                        (
                            [id] => 5
                            [comment] => e
                            [parent_id] => 2
                            [children] => Array
                                (
                                    [7] => Array
                                        (
                                            [id] => 7
                                            [comment] => g
                                            [parent_id] => 5
                                            [children] => Array
                                                (
                                                    [8] => Array
                                                        (
                                                            [id] => 8
                                                            [comment] => h
                                                            [parent_id] => 7
                                                            [children] => Array
                                                                (
                                                                    [9] => Array
                                                                        (
                                                                            [id] => 8
                                                                            [comment] => h
                                                                            [parent_id] => 8
                                                                            [children] => Array
                                                                                (
                                                                                    [10] => Array
                                                                                        (
                                                                                            [id] => 8
                                                                                            [comment] => h
                                                                                            [parent_id] => 9
                                                                                            [depth] => 0
                                                                                            [child_count] => 0
                                                                                            [children] => 
                                                                                        )

                                                                                )

                                                                            [depth] => 1
                                                                            [child_count] => 1
                                                                        )

                                                                )

                                                            [depth] => 2
                                                            [child_count] => 1
                                                        )

                                                )

                                            [depth] => 3
                                            [child_count] => 1
                                        )

                                )

                            [depth] => 4
                            [child_count] => 1
                        )

                    [6] => Array
                        (
                            [id] => 6
                            [comment] => f
                            [parent_id] => 2
                            [depth] => 0
                            [child_count] => 0
                            [children] => 
                        )

                )

            [depth] => 5
            [child_count] => 2
        )

)

你需要一点递归

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);
函数遍历数组($array)
{
回声“
    ”; foreach($array作为$element) { 回音“
  • ”; if(isset($element['comment'])) { echo$element['comment']; } if(is_数组($element['children'])和计数($element['children'])>0) { 遍历数组($element['children']); } 回音“
  • ”; } 回声“
”; } 遍历数组($大数组);
在这里,myhierTree()函数默认打印嵌套的ulol列表,对于嵌套数组的深度不确定的情况,对于问题中提供的示例数组,此函数将开箱即用

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);
这个稍加修改的版本将根据列表元素打印一个深度类。
因此,您可以通过一个简单的CSS规则(如
ul.depth1>LI)来定位他们的*LI*s{…

这对你很好,madphp。如果你在编写代码时遇到任何问题,请告诉我们;@Ryan我不想因为我的尝试而弄脏了水;-)哈哈,谢谢。它没有经过测试…=/所以希望它能工作,但至少应该是一个好的开始。我已经解决了两个问题。我需要一个堆栈溢出测试PHP问题的环境。有点像JSFIDLE的东西恰好是因为这个原因,我前几天买了PHPFiddle.com。请继续关注;)您可以使用is_数组而不是isseties,但是如果children元素不是数组,而是字符串,或者更糟的是,是数字呢?嘿,Mario。我如何在
  • 中插入一个类以便更改style取决于元素的深度。我会使用CSS直接后代选择器重新编译,无论如何,我已经修改了我的答案,以提供您要求的功能。忘了提及,我的编辑在列表元素本身中插入类,而不是项,因为您可以更容易地访问项,代码将更小。不需要相同的类对于很多lis来说,他们都处于同一水平。我还没有测试过。我这周会的。来吧,你一直在做什么,停止聚会和工作……天啊。
    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";
    }