使用物化路径将平面数组转换为嵌套数组

时间:2015-01-31 20:52:07

标签: php arrays mongodb materialized-path-pattern

我有一些类别需要转换为嵌套数组(树)。我使用物化路径来制作树,我在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
        )
)

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

1 个答案:

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