【问题标题】:Recursivly ordenate array with id and parent_id structure具有 id 和 parentid 结构的递归纵坐标数组
【发布时间】:2012-01-09 15:31:41
【问题描述】:

所以,我得到了这个数组(从数据库收集数据):

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
        )

    [1] => Array
        (
            [id] => 2
            [parent_id] => 0
        )

    [2] => Array
        (
            [id] => 3
            [parent_id] => 2
        )

    [3] => Array
        (
            [id] => 4
            [parent_id] => 2
        )

    [4] => Array
        (
            [id] => 5
            [parent_id] => 4
        )
)

我正在尝试像这样创建和排序数组:

Array
(
    [1] => Array
        (
            [parent_id] => 0
        )

    [2] => Array
        (
            [parent_id] => 0
            [children] => Array
            (
                [3] => Array
                    (
                        [parent_id] => 2
                    )

                [4] => Array
                    (
                        [parent_id] => 2
                        [children] => Array
                        (
                            [5] => Array
                                (
                                    [parent_id] => 4
                                )
                        )
                    )
            )
        )
)

我尝试使用以下代码:

function placeInParent(&$newList, $item)
{
    if (isset($newList[$item['parent_id']]))
    {
        $newList[$item['parent_id']]['children'][$item['id']] = $item;
        return true;
    }

    foreach ($newList as $newItem)
    {
        if (isset($newItem['children']))
        {
            if (placeInParent($newItem['children'], $item))
            {
                return true;
            }
        }
    }
    return false;
}

$oldList = (first array above)

$newList = array();

foreach ($oldList as $item)
{
    if ($item['parent_id'] == 0)
    {
        $newList[$item['id']] = $item;
    }
    else
    {
        placeInParent($newList, $item);
    }
}

但问题是我只得到了数组的前 2 个级别!最后一个丢失了..我的有序数组结果如下:

Array
(
    [1] => Array
        (
            [parent_id] => 0
        )

    [2] => Array
        (
            [parent_id] => 0
            [children] => Array
                (
                    [3] => Array
                        (
                            [parent_id] => 2
                        )

                    [4] => Array
                        (
                            [parent_id] => 2
                        )
                )
        )
)

我就是不知道哪里搞砸了:\帮助?

【问题讨论】:

    标签: php arrays recursion


    【解决方案1】:

    借助引用树内节点的索引,您可以在不递归的情况下做到这一点:

    $arr = array(
        array('id'=>1, 'parent_id'=>0),
        array('id'=>2, 'parent_id'=>0),
        array('id'=>3, 'parent_id'=>2),
        array('id'=>4, 'parent_id'=>2),
        array('id'=>5, 'parent_id'=>4),
    );
    
    // array to build the final hierarchy
    $tree = array(
        'children' => array(),
        'path'     => array()
    );
    
    // index array that references the inserted nodes
    $index = array(0=>&$tree);
    
    foreach ($arr as $key => $val) {
        // pick the parent node inside the tree by using the index
        $parent = &$index[$val['parent_id']];
        // append node to be inserted to the children array
        $node = array(
            'parent_id' => $val['parent_id'],
            'path'      => $parent['path'] + array($val['id'])
        );
        $parent['children'][$val['id']] = $node;
        // insert/update reference to recently inserted node inside the tree
        $index[$val['id']] = &$parent['children'][$val['id']];
    }
    

    您要查找的最终数组位于$tree['children']

    【讨论】:

    • 嗯...它的工作原理很完美!没想到会“这么简单”!我想我“想多了”,并且采取了艰难的(而且据说是错误的)方式来做到这一点。唯一的问题是我没有完全理解代码:\你能解释一下吗?
    • @MARCOGPINTO 你不明白什么?
    • 我有点理解逻辑,但是让我们想象一下,我想添加一个字段来保存该节点的路径,例如,对于节点 5,我保存 2-4-5。我不知道我可以在那个 foreach 中做到这一点的确切位置.. :\
    • 几分钟后,通过一些回声来了解在 foreach 上究竟发生了什么,我了解了所有逻辑以及如何使用它!非常感谢你的帮助! :)
    • @MARCOGPINTO:可以用类似的方式组合绝对路径:由于每个新节点都作为树内现有节点的子节点插入,因此通过使用父节点的路径并通过新节点的 ID 扩展它。例如:$path = $parent['path'] + array($val['id']); 而根的路径是一个空数组 ($tree = array('children' => array(), 'path' => array()))。
    猜你喜欢
    • 1970-01-01
    • 2012-05-06
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多