简体   繁体   中英

Creating a dynamic tree array from another array

I have an array, lets call it $childrenIds, it outputs as follows:

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
    )

); What i need to do is create a new array from this, where the eg [74252] is ignored, but the children of each sub array are pathed...

So using this example my output would be something like: array(

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

So there will not always be 4 sub array elements, that is dynamic...

The parents are just based on the index of that array. So... index[0] is the parent of index[1], index[1] is the parent of index[2] and so forth.

Also, I want to end up with all UNIQUE paths, no duplicate values per path.

Hopefully I have explained this clearly, been searching for a few hours and can't find a solution that meets all of my requires, if I overlooked one, I apologize in advance.

Thanks

UPDATE

As opposed to passing an array I ended up passing an underscore delimited string, then using this function:

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;
}

found @: http://project-2501.net/index.php/2007/10/explodetree/
AND: http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/

I was able to obtain the desired result.

For the first element in the array:

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

The paths represented are basically

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

You could probably solve with something like this (not tested). The explodeTree function is from http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/ and I'm assuming that it works as advertised. Never used it myself.

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

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

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

Code: (UPDATED)

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

Input:

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

Output:

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

                )

        )

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

                )

        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM