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.
(1753->1757->1758->1760, 1754->1755->1758->1763, ...)? Be careful so that we are talking about the same thing....