dpw5865 2011-12-22 22:51
浏览 93
已采纳

递归函数从分层数组生成平面(但带有标题嵌套)数组

Another recursion question.

What I've tried to develop for the past few hours is a function that will let me generate a flat array of the same pages, but with the children having an identifier in front of them, (i.e. "— Sub Page, — — Sub Sub Page, etc").

I have a hierarchical array of pages, with each child page nested inside of their parent page. Here's an example of their output:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
            [title] => Parent Page
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 12
                            [parent_id] => 1
                            [title] => Another Sub Page
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [parent_id] => 1
                            [title] => Sub Page
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 7
                                            [parent_id] => 3
                                            [title] => Sub Sub Page
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 8
            [parent_id] => 0
            [title] => Another Parent Page
        )

)

I've been able to get it working until I reach the second level of the recursion and then it's still generating only one —. I want to make the solution recursive.

What I'm looking for in the final output using the example above is:

Array
(
    [1] => Parent Page
    [12] => — Another Sub Page
    [3] => — Sub Page
    [7] => — — Sub Sub Page
    [8] => Another Parent Page
)
  • 写回答

3条回答 默认 最新

  • dongshuogai2343 2011-12-22 23:08
    关注
    function generate_array(array $arr, &$output = array(), $index = 0)
    {
        foreach($arr as $item)
        {
            $output[$item['id']] = str_repeat('— ', $index) . $item['title'];
            if(isset($item['children']))
            {
                generate_array($item['children'], $output, $index + 1);
            }
        }
        return $output;
    }
    
    $output = generate_array($arr);
    

    The data stored in $output would be:

    Array
    (
        [1] =>  Parent Page
        [12] => — Another Sub Page
        [3] => — Sub Page
        [7] => — — Sub Sub Page
        [8] =>  Another Parent Page
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀