1

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 e.g. [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.

8
  • How are the children of each item "pathed"? What's the logic? Why - in the resulting array - 1753 has 1755 ans 1757 as children? (and not something else) Commented Apr 11, 2012 at 3:44
  • Sorry ... 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. Commented Apr 11, 2012 at 3:46
  • So you need something like (1753->1757->1758->1760, 1754->1755->1758->1763, ...)? Be careful so that we are talking about the same thing.... Commented Apr 11, 2012 at 3:48
  • Yes I believe so, however the next would be 1754-1756->1758->1763 Commented Apr 11, 2012 at 3:50
  • OK, let's say we take 3 elements of your initial MAIN array (..252,..238,..476) ... and convert them... how many elements in our NEW MAIN array will that make? Commented Apr 11, 2012 at 3:51

2 Answers 2

1

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, "/");
Sign up to request clarification or add additional context in comments.

1 Comment

You set me on the right path... i found a solution via google here... project-2501.net/index.php/2007/10/explodetree, thanks a lot.
0

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
                        )

                )

        )

)

13 Comments

Well that is the desired output! However there are many more than the 5 elements of my main array that I shared with you, and the number of sub elements will also vary. I appreciate the effort though.
@Jim Give me a sec, and I'll make it handle variable sub elements too.
@Jim I just updated my answer... it'll now work for ANY elements in the main array, and for ANY number of sub-elements... :-)
wow, first off thanks a lot for the time / effort... unfortunately its not quite there, so the problem is that it needs to detect if the parent array exists down each path, and if it does, not overwrite it, but a append a child with the path
@Jim Maybe try giving me a precise example?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.