0

如何获取以下内容并将其作为漂亮的格式化选择下拉菜单回显,包括每个级别的“--”?

Array
(
    [0] => stdClass Object
        (
            [id] => 2
            [user_id] => 6
            [title] => First Category
            [parent_id] => 0
        )

    [1] => stdClass Object
        (
            [id] => 4
            [user_id] => 6
            [title] => HTML Times
            [parent_id] => 0
        )

    [2] => stdClass Object
        (
            [id] => 5
            [user_id] => 6
            [title] => Email Ninja
            [parent_id] => 0
        )

    [3] => stdClass Object
        (
            [id] => 6
            [user_id] => 6
            [title] => Taco Night
            [parent_id] => 0
        )

    [4] => stdClass Object
        (
            [id] => 7
            [user_id] => 6
            [title] => SSH
            [parent_id] => 0
            [childs] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 10
                            [user_id] => 6
                            [title] => Root Commands
                            [parent_id] => 7
                        )

                    [1] => stdClass Object
                        (
                            [id] => 11
                            [user_id] => 6
                            [title] => Shell
                            [parent_id] => 7
                        )

                    [2] => stdClass Object
                        (
                            [id] => 12
                            [user_id] => 6
                            [title] => Linux
                            [parent_id] => 7
                        )

                    [3] => stdClass Object
                        (
                            [id] => 13
                            [user_id] => 6
                            [title] => Windows
                            [parent_id] => 7
                        )

                )

        )

    [5] => stdClass Object
        (
            [id] => 8
            [user_id] => 6
            [title] => Dinner Tonight
            [parent_id] => 0
            [childs] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 9
                            [user_id] => 6
                            [title] => Mexican
                            [parent_id] => 8
                            [childs] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 14
                                            [user_id] => 6
                                            [title] => Tacos
                                            [parent_id] => 9
                                        )

                                )

                        )

                )

        )

)

我知道这个话题已经被广泛报道了,但对我来说并没有太多的意义。经过两天的阅读......阅读......阅读......我终于能够从我的数据库中获取这些数据。现在,我似乎无法让它比第一类更深刻。

感谢您的帮助和耐心。

这是我目前得到父母的方式:

$级别 = 0;

$categories = $this->mycrate->build_categories();

    echo '<select class="input-block-level">';

    function RecursiveWrite($categories) {
        foreach ($categories as $category) {
            echo '<option value="'.$category->id.'">'.$category->title.'</option>';
            RecursiveWrite($category->parent_id);
        }
    }

    RecursiveWrite($categories);

    echo '</select>';
4

1 回答 1

1
function RecursiveWrite($categories) {
    foreach ($categories as $category) {
        echo '<option value="'.$category->id.'">'.$category->title.'</option>';
        RecursiveWrite($category->childs);
    }
}

RecursiveWrite($categories);

尝试开始,递归部分需要在子元素上调用,而不是父 ID。

于 2013-07-14T22:10:02.840 回答