1

我已经坚持了两天了,我完全被困住了。谷歌和 SO 无法进一步帮助我,我忽略了一些东西....

这就是我要的

Home
Typography
404 Page not found
The company
The company/Team
The company/Team/Rick
The company/Team/Rick/Opleiding
The company/Team//Rob
The company/Office
The company/Office/Contact
The company/Office/Route

这是我的数组

    Array
(
    [0] => Array
        (
            [id] => 55
            [parent_id] => 0
            [title] => Home
            [sub] => Array
                (
                )

        )

    [1] => Array
        (
            [id] => 27
            [parent_id] => 0
            [title] => Typography
            [sub] => Array
                (
                )

        )

    [2] => Array
        (
            [id] => 56
            [parent_id] => 0
            [title] => 404 Page not found
            [sub] => Array
                (
                )

        )

    [3] => Array
        (
            [id] => 68
            [parent_id] => 0
            [title] => The company
            [sub] => Array
                (
                    [0] => Array
                        (
                            [id] => 73
                            [parent_id] => 68
                            [title] => Team
                            [sub] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 74
                                            [parent_id] => 73
                                            [title] => Rick
                                            [sub] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 79
                                                            [parent_id] => 74
                                                            [title] => Opleiding
                                                            [sub] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 75
                                            [parent_id] => 73
                                            [title] => Rob
                                            [sub] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 76
                            [parent_id] => 68
                            [title] => Office
                            [sub] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 78
                                            [parent_id] => 76
                                            [title] => Contact
                                            [sub] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 77
                                            [parent_id] => 76
                                            [title] => Route
                                            [sub] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

这是我到目前为止所拥有的

public function BuildTitle($array, &$parents = null)
    {
        $html = '';
        foreach($array as $page)
        {
            if($page['parent_id'] != 0)
            {
                $html .= $parents.'/'.$page['title'].'<br/>';
                $parents = $parents.'/'.$page['title'];
            }
            else
            {
                $html .= $page['title'].'<br/>';
                $parents = $page['title'];
            }

            $html .= $this->BuildTitle($page['sub'], $parents);
        }
        return $html;
    }

这又回来了

Home
Typography
404 Page not found
The company
The company/Team
The company/Team/Rick
The company/Team/Rick/Opleiding
The company/Team/Rick/Opleiding/Rob
The company/Team/Rick/Opleiding/Rob/Office
The company/Team/Rick/Opleiding/Rob/Office/Contact
The company/Team/Rick/Opleiding/Rob/Office/Contact/Route

父/子关系是无限的。我在做什么错/错过了什么?提前致谢!

4

1 回答 1

0

$parents在每次迭代中修改相同的值。因此,只要父级不为 0,它只会将当前子级附加到任何路径。

想象一下,例如,您有以下场景:

foo
   bar
   baz

处理时foo,它会以$parents = 'foo'. 然后它遍历每个孩子,更新$parents

  • bar使$parents = $parents . '/' . $page['title'] = 'foo' . '/' . 'bar' = 'foo/bar'.
  • baz使$parents = $parents . '/' . $page['title'] = 'foo/bar' . '/' . 'bar' = 'foo/bar/baz'.

你想要的是这样的:

public function BuildTitle($array, $parents = null)
    {
        $html = '';
        foreach($array as $page)
        {
            if($page['parent_id'] != 0)
            {
                $html .= $parents.'/'.$page['title'].'<br/>';
                $new_parents = $parents.'/'.$page['title'];
            }
            else
            {
                $html .= $page['title'].'<br/>';
                $new_parents = $page['title'];
            }

            $html .= $this->BuildTitle($page['sub'], $new_parents);
        }
        return $html;
    }
于 2012-08-03T12:08:43.463 回答