【问题标题】:Traverse Array and Display In Bullet Points遍历数组并以项目符号显示
【发布时间】:2011-09-03 05:00:12
【问题描述】:

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

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
        )

)

【问题讨论】:

  • 对你有好处,madphp。如果您在尝试编写代码时遇到任何问题,请告诉我们 ;)
  • @Ryan 我不想通过展示我的尝试来搅浑水。 ;-)

标签: php multidimensional-array


【解决方案1】:

你需要一点递归

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);

【讨论】:

  • 哈哈,是的,谢谢。它未经测试... =/ 所以希望它可以工作,但它至少应该是一个好的开始
  • 实际上我已经解决了两个问题。我需要一个针对 PHP 问题的堆栈溢出测试环境。有点像 jsfiddle 的东西
  • 恰巧我前几天因为这个原因购买了 PHPFiddle.com。敬请期待;)
  • 你可以用 is_array 代替 isset
  • 是的,但是如果子元素不是一个数组,而是一个字符串,或者更糟的是一个数字呢?
【解决方案2】:

给你,我的 hierTree() 函数默认打印嵌套的 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);

这个函数的输出将被很好地缩进以便于阅读。

此外,如果您执行hierTree($tree, 'ol'); you will get an ordered list. Id you do hierTree($tree, 'ol', 'id');,您将获得一棵有序树,并且将打印 id 字段而不是默认的 comment 字段。


插入类。

如果您想为每个 list 元素设置不同的类,这样您就可以更轻松地在 CSS 上设置样式。 (虽然我会推荐使用 CSS 直接后代选择器(“>”))

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";
}

这个稍作修改的版本将为每个 list 元素打印一个 depthN 类。 因此,您可以通过简单的 CSS 规则(例如 ul.depth1 &gt; li { ...)定位他们的 *LI*s。

【讨论】:

  • 嘿马里奥。如何在
  • 中插入一个类,以便可以根据元素的深度更改样式。
  • 我会推荐使用 CSS 直接后代选择器,无论如何,我已经修改了我的答案以提供您要求的功能。
  • 忘了提及,我的编辑在列表元素本身插入类而不是项目,因为您可以更轻松地访问项目并且代码会更小。许多 lis 都在同一级别时,不需要同一个类。
  • 尚未对此进行测试。这周我会的。
  • 来吧,你一直在做什么,退出聚会和工作......天哪。
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签