遍历数组并在项目符号点中显示 [英] Traverse Array and Display In Bullet Points

查看:89
本文介绍了遍历数组并在项目符号点中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历此数组并显示注释"作为要点.

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

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
        )

)

推荐答案

在这里,默认情况下,我的 hierTree()函数会打印嵌套的 ul ol 列出,对于不确定的嵌套数组深度,此函数将对您问题中提供的示例数组开箱即用.

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.

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


插入课程.

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.

如果您希望每个 list 元素具有不同的类,以便可以更轻松地在CSS上设置样式. (尽管我建议使用CSS直接后代选择器(>"))

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

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

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 { ....

这篇关于遍历数组并在项目符号点中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆