简体   繁体   English

努力将数组结构转换为 PHP 中的递归树

[英]Struggling to convert an array structure to a recursive tree in PHP

I've been pulling my hair out now for a few hours trying to convert a flat array that contains categories into a multi-level tree, but I'm struggling to write a function that can do it, I've been trying for around 4 hours now and I'm at my wits end.我已经花了几个小时试图将包含类别的平面数组转换为多级树,但我正在努力编写一个可以做到的 function,我一直在尝试现在有4个小时,我已经不知所措了。

I'm pretty new to Algorithms and working with this kind of data structure so I'm hoping someone can point me in the right direction (I've tried several solutions listed here on SO to no avail).我对算法和使用这种数据结构很陌生,所以我希望有人能指出我正确的方向(我已经尝试了这里列出的几种解决方案,但无济于事)。

Most seem to require an id field of sorts but unfortunately my data structure does not have one.大多数似乎都需要一个 id 字段,但不幸的是我的数据结构没有。

The original data structure looks like this:原始数据结构如下所示:

        $data = [
            ['name'=>"vehicles",'parent'=>'root'],
            ['name'=>"cars",'parent'=>'vehicles'],
            ['name'=>"Porsche",'parent'=>'cars'],
            ['name'=>"Volvo",'parent'=>'cars'],
            ['name'=>"Ford",'parent'=>'cars'],
            ['name'=>"Skoda",'parent'=>'cars'],
            ['name'=>"Kawasaki",'parent'=>'bikes'],
            ['name'=>"Harley",'parent'=>'bikes'],
            ['name'=>"Electronics",'parent'=>'root'],
            ['name'=>"Mobile Phones",'parent'=>'Electronics'],
            ['name'=>"Nokia",'parent'=>'Mobile Phones'],
            ['name'=>"Apple",'parent'=>'Mobile Phones'],
            ['name'=>"Motorola",'parent'=>'Mobile Phones'],
            ['name'=>"Google",'parent'=>'Mobile Phones']
        ];

And I'm trying to make the output look like this:我正在尝试使 output 看起来像这样:

        $output=[
            [
                'name'=>"root",
                'children'=>[
                    [
                        "name"=>"vehicles",
                        "children"=>[
                            [
                                "name"=>"cars",
                                "children"=>[
                                    ["name"=>"Porsche"],
                                    ["name"=>"Volvo"],
                                    ["name"=>"Ford"],
                                    ["name"=>"Skoda"],
                                ]
                            ],
                        ]
                    ],
                    [
                        "name"=>"Electronics",
                        "children"=>[
                            [
                                "name"=>"Mobile Phones",
                                "children"=>[
                                    ["name"=>"Nokia"],
                                    ["name"=>"Apple"],
                                    ["name"=>"Google"],
                                    ["name"=>"Motorola"],
                                ]

                            ]

                        ]
                    ]
                ]]
        ];

If someone could point me in the right direction I'd be very grateful... Hell, at this point I'd even pay for the solution.如果有人能指出我正确的方向,我将非常感激......地狱,在这一点上,我什至会为解决方案付费。

EDIT: I found a solution, thanks to everyone who helped me out.编辑:我找到了一个解决方案,感谢所有帮助我的人。


    function buildTree(array $elements, $parentId = 0)
    {
        $branch = array();
        foreach ($elements as $element) {
            if ($element['parent'] == $parentId) {
                $children = $this->buildTree($elements, $element['name']);
                if (!empty($children)) {
                    $element['children'] = $children;
                }
                $branch[] = $element;
            }
        }
        return $branch;
    }

function buildTree(array $elements, $parentId = 0)
    {
        $branch = array();
        foreach ($elements as $element) {
            if ($element['parent'] == $parentId) {
                $children = $this->buildTree($elements, $element['name']);
                if (!empty($children)) {
                    $element['children'] = $children;
                }
                $branch[] = $element;
            }
        }
        return $branch;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM