从多维数组中列出子父项

时间:2012-08-03 12:03:03

标签: php multidimensional-array

我已经在这两天了,我完全陷入了困境。 Google和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

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

1 个答案:

答案 0 :(得分: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;
    }