PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am trying to implement a simple function to traverse an array while doing so applying a 'root' node, so wherever I am in the tree I can determine its parent node and reverse back up the tree if needed.
The following doesn't wortk and I have a hunch it's because $array should be a static or I am missing something with references because the resulting array is huge, and root should merely reference the parent node and the result (when dumped) shouold say something like **recurive**.
I am not entirely sure of what you are trying to change in the structure of the passed in array but there is a problem here.
Each iteration you append the entire list into the new array (redundant). Then you go and add the next element to the new array (more redundancy).
Maybe you can find the solution but If you could post a simple example of an input and output, I could try to be of more help.
Array
(
['root'] => **recursive**
[0] => TEST
[1] => Array
(
['root'] => **recursive**
[0] => TEST
[1] => TEST
[2] => 4321
)
[2] => Array
(
['root'] => **recursive**
[0] => TEST
[1] => Array
(
['root'] => **recursive**
[0] => TEST
[1] => Array
(
['root'] => **recursive**
[0] => TEST
[1] => part
[2] =>
)
[2] => Array
(
['root'] => **recursive**
[0] => TEST
[1] => TEST
[2] => 1234
)
)
)
)
I have done this before, so I am certain the array nodes can 'reference' other nodes and when you print_r() the result all referenced nodes should say **recursive** or something that that affect.
I'm just wondering now, if whether the $temp array being recreated upon each invocation of the method...cause references do not appear to be working at all. I effectively get a cloned snapshop of all the parent items instead of a reference to only the first level up.
I think the problem is that you are pointing to the original array, not the new one. Also, I would move the root statement outside of the loop, as it is now it just keeps giving it the same value over and over.
This snippet almost works though the references are pointing at the original which results in endless loops. I need ROOT to point to one item above that.
I just made a couple modifications to your original one. I have not tested this at all.
In your latest post, which of those 2 is the closest to working? In both you never do this else for if the element is not an array. Maybe adding that will help. You also really should not need the static depth, there must be a better solution.
function make_tree_bidirectional(&$tree) {
foreach ($tree as &$elt) { // iterate by reference - important
if (is_array($elt)) {
$elt['root'] =& $tree;
make_tree_bidirectional($elt);
}
unset($elt); // unset the reference - otherwise next iteration will destroy current element
}
}
make_tree_bidirectional($your_tree); // note that $your_tree is modified in place - no need to return anything