0

我有一些类别需要转换为嵌套数组(树)。我使用物化路径来制作一棵树,我在 PHP 中工作。这是我所拥有的 print_r:

Array
(
    [0] => Array
        (
            [_id] => mac
            [name] => Mac
            [path] => null
        )

    [1] => Array
        (
            [_id] => ipod
            [name] => iPod
            [path] => null
        )

    [2] => Array
        (
            [_id] => imac
            [name] => iMac
            [path] => ,mac,
        )

    [3] => Array
        (
            [_id] => imac2001
            [name] => iMac 2001
            [path] => ,mac,imac,
        )

    [4] => Array
        (
            [_id] => imac2002
            [name] => iMac 2002
            [path] => ,mac,imac,
        )

    [5] => Array
        (
            [_id] => imac2003
            [name] => iMac 2003
            [path] => ,mac,imac,
        )

)

我试图做到这一点,但我迷失了递归函数的整个想法。

更新 :

这是我最终想要的:

Array
(
    [0] => Array
        (
            [_id] => mac
            [name] => Mac
            [path] => null
            [children] =>
                [0] => Array
                    (
                        [_id] => imac
                        [name] => iMac
                        [path] => ,mac,
                        [children] =>
                            [0] => Array
                                (
                                    [_id] => imac2001
                                    [name] => iMac 2001
                                    [path] => ,mac,imac,
                                )

                            [1] => Array
                                (
                                    [_id] => imac2002
                                    [name] => iMac 2002
                                    [path] => ,mac,imac,
                                )

                            [2] => Array
                                (
                                    [_id] => imac2003
                                    [name] => iMac 2003
                                    [path] => ,mac,imac,
                                )
                    )
        )

    [1] => Array
        (
            [_id] => ipod
            [name] => iPod
            [path] => null
        )
)

我想向您展示我已经编码的内容,但它离工作还很远,所以在我看来没什么好看的。

4

1 回答 1

0

我找到了解决方案。它对我有用,所以这里是:

<?php

$categories = Array
(
    [0] => Array
        (
            ["_id"] => "mac"
            ["name"] => "Mac"
            ["path"] => null
        )

    [1] => Array
        (
            ["_id"] => "ipod"
            ["name"] => "iPod"
            ["path"] => null
        )

    [2] => Array
        (
            ["_id"] => "imac"
            ["name"] => "iMac"
            ["path"] => ",mac,"
        )

    [3] => Array
        (
            ["_id"] => "imac2001"
            ["name"] => "iMac 2001"
            ["path"] => ",mac,imac,"
        )

    [4] => Array
        (
            ["_id"] => "imac2002"
            ["name"] => "iMac 2002"
            ["path"] => ",mac,imac,"
        )

    [5] => Array
        (
            ["_id"] => "imac2003"
            ["name"] => "iMac 2003"
            ["path"] => ",mac,imac,"
        )
)

$categoriesNested = sortTree($categories);

function sortTree(&$categories, $parent = null)
{
    $result = array();

    foreach($categories as $key => $cat)
    {
        if($parent == null)
        {
            $temp = $cat;
            unset($categories[$key]);
            $temp["children"] = $this->sortTree($categories, $cat);
            $result[] = $temp;
        }
        else
        {
            $tempPath = array_values(array_filter(explode(',', $cat["path"])));
            $directParent = array_pop($tempPath);
            if ($parent["_id"] == $directParent)
            {
                $temp = $cat;
                unset($categories[$key]);
                $temp["children"] = $this->sortTree($categories, $cat);
                $result[] = $temp;
            }
        }
    }

    if (empty($result))
        return null;
    else
        return $result;
}
于 2015-02-01T17:26:24.007 回答