从另一个数组创建动态树数组

时间:2012-04-11 03:40:00

标签: php arrays

我有一个数组,我们称之为$ childrenIds,输出如下:

array(

[74252] => Array
    (
        [0] => 1753
        [1] => 1757
        [2] => 1758
        [3] => 1760
    )

[74238] => Array
    (
        [0] => 1753
        [1] => 1755
        [2] => 1758
        [3] => 1761
    )

[76476] => Array
    (
        [0] => 1754
        [1] => 1755
        [2] => 1758
        [3] => 1763
    )

[76478] => Array
    (
        [0] => 1754
        [1] => 1756
        [2] => 1758
        [3] => 1763
    )

[76480] => Array
    (
        [0] => 1754
        [1] => 1757
        [2] => 1758
        [3] => 1763
    )

[74253] => Array
    (
        [0] => 1753
        [1] => 1757
        [2] => 1759
        [3] => 1760
    )

); 我需要做的是从中创建一个新的数组,例如, [74252]被忽略, 但是每个子阵列的子节点都被修改了......

所以使用这个例子我的输出将是这样的:     阵列(

[1753] => Array
(
    [1757] => Array
    (
        [1758] => Array
        (
            1760
        ),
        [1759] => Array
        (
            1760
        ),
    )
    [1755] =>  Array
        (
            1758 =>  Array
            (
                1761
            )
        )
    )
),
[1754] => Array
(
    [1755] => Array
    (
        [1758] => Array
        (
            1763
        )
    ),
    [1756] => Array
    (
        [1758] => Array
        (
            1763
        )
    ),
    [1757] => Array
    (
        [1758] => Array
        (
            1763
        )
    )
)
);

所以不会总是有4个子数组元素,即动态...

父母只是基于该数组的索引。所以...... index [0]是索引[1]的父级,索引[1]是索引[2]的父级,依此类推。

另外,我希望最终得到所有UNIQUE路径,每条路径没有重复值。

希望我已经清楚地解释了这一点,一直在搜索几个小时,找不到满足我所有要求的解决方案,如果我忽略了一个,我会事先道歉。

由于

更新

与传递数组相反,我最后传递了一个下划线分隔的字符串,然后使用这个函数:

function explodeTree($array, $delim = '/')
{
$tree = array();

foreach($array as $elem)
{
    //  Split our string up, and remove any blank items
    $items = explode($delim, $elem);
    $items = array_diff($items, array(''));

    //  current holds the current position in the tree
    $current = &$tree;

    foreach($items as $item)
    {
        //  If we've not created this branch before, or there is
        //  a leaf with the same name, then turn it into a branch
        if(!isset($current[$item]) || !is_array($current[$item]))
        {
            $current[$item] = array();
        }

        //  Update our current position to the branch we entered
        //  (or created, depending on the above if statement)
        $current = &$current[$item];
    }

    //  If the last value in this row is an array with 0 elements
    //  then (for now) we will consider it a leaf node, and set it
    //  to be equal to the string representation that got us here.
    if(count($current) == 0)
    {
        $current = $elem;
    }
}

return $tree;
}

发现@: http://project-2501.net/index.php/2007/10/explodetree/
和: http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/

我能够获得所需的结果。

2 个答案:

答案 0 :(得分:1)

对于数组中的第一个元素:

[74252] => Array
    (
        [0] => 1753
        [1] => 1757
        [2] => 1758
        [3] => 1760
    )

所代表的路径基本上是

[0] => 1753
[1] => 1753/1757
[2] => 1753/1757/1758
[3] => 1753/1757/1758/1760

你可以用这样的东西解决(未经测试)。 explodeTree函数来自http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/,我假设它的工作方式与广告一样。从来没有用过它。

$pathArray = array();
foreach($startArray as $subArray) {
    $pathStr = '';

    foreach($subArray as $v) {
        $pathStr = $pathStr.'/'.$v;
        $pathArray[]=$pathStr;
    }        
}

$pathArray = array_unique($pathArray);
$treeArray = explodeTree($pathArray, "/");

答案 1 :(得分:0)

代码: (更新)

$newarr = array();

function getElem($sub,$n)
{
    $res = array();

    if ($n==count($sub)-1)
        $res[]=$sub[$n];
    else
        $res[$sub[$n]] = getElem($sub,$n+1);

    return $res;
}

foreach ($arr as $subarr)
{
    $newarr[$subarr[0]] = getElem($subarr,1);
}

print_r($newarr);

输入:

$arr=
array(

74252 => Array
    (
        0 => 1753,
        1 => 1757,
        2 => 1758,
        3 => 1760
    ),

74238 => Array
    (
        0 => 1753,
        1 => 1755,
        2 => 1758,
        3 => 1761
    ),

76476 => Array
    (
        0 => 1754,
        1 => 1755,
        2 => 1758,
        3 => 1763
    ),

76478 => Array
    (
        0 => 1754,
        1 => 1756,
        2 => 1758,
        3 => 1763
    )
);

输出

Array
(
    [1753] => Array
        (
            [1755] => Array
                (
                    [1758] => Array
                        (
                            [0] => 1761
                        )

                )

        )

    [1754] => Array
        (
            [1756] => Array
                (
                    [1758] => Array
                        (
                            [0] => 1763
                        )

                )

        )

)