Recursivly ordenate阵列ID和结构PARENT_ID [英] Recursivly ordenate array with id and parent_id structure

查看:94
本文介绍了Recursivly ordenate阵列ID和结构PARENT_ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我得到这个阵列(将数据从数据库收集):

So, I got this array (data gather from a database):

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
        )
)

和我试图创建和有序排列是这样的:

and I'm trying to create and ordered array like this:

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
                                )
                        )
                    )
            )
        )
)

和我试着用下面的code:

and I tried with the following code:

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级的阵!最后一个是丢失了..和我的有序阵列原来是这样的:

but the problem is that I only get the first 2 levels of the array! The last one is lost.. and my ordered array turns out like this:

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

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

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

我只是不能在那里我搞乱了:\\帮助

I just can't get where I'm messing up :\ help?

推荐答案

可以做到这一点没有引用这些树中的​​节点创建索引的帮助递归:

You can do this without recursion with the help of an index that references the nodes inside the tree:

$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']];
}

您正在寻找的最后阵列在 $树['孩子']

The final array you are looking for is in $tree['children'].

这篇关于Recursivly ordenate阵列ID和结构PARENT_ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆