douyan2970 2013-07-14 20:41
浏览 40
已采纳

PHP格式多维对象选择下拉深度

How can I take the following and echo it as a beautiful formatted select drop down including '--' for each level?

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
                                        )

                                )

                        )

                )

        )

)

I know this topic is pretty heavily covered, but it's not making a whole lot of since to me. After two days of reading... and reading... and reading... I was finally able to get this data out of my database. Now, I can't seem to get it to echo out any deeper than the first category.

Thanks for the assistance and patience.

Here's how I'm getting the parents currently:

$level = 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>';
  • 写回答

1条回答 默认 最新

  • dongqiu3254 2013-07-14 22:10
    关注
    function RecursiveWrite($categories) {
        foreach ($categories as $category) {
            echo '<option value="'.$category->id.'">'.$category->title.'</option>';
            RecursiveWrite($category->childs);
        }
    }
    
    RecursiveWrite($categories);
    

    Try that to start, the recursive part needs to be called on the children elements, not the parent ID.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题