Forum Moderators: coopster
Anyone know where I can get some tips of navigating and editing tree structures (ie variable levels of nesting of arrays and associative arrays).
The navigating is not really a problem, I can just use a recursive function for that bit.
But lets say I have basically an address of three components (in reality it could be more, could be less), where each components refers to a different node in the array structure. I need to be able to navigate the array using my address, and then edit only that part of the array. In reality I would get to last node, use that ID number to query a databse to pull out the child nodes then add those child nodes onto the array structure.
Thanks
Yeah, I've been scratching my head for a few hours over this one now.
The main trouble I am having though is not accessing the right part of array, but then adding some new data to that part of the array without messing up the rest of the array.
I had thought I should be able to do it using recursive function and references, ie keep passing in the node id until I get the part of the array I want.
The trouble I got though was my reference was basically making my original array the same as the branch I had extracted. I am sure I should have been able to do it this way but I couldn't get the referneces right.
Anyhow, I think I have found a solution. Since I don't know the depth of the array I nee something quote dynamic. I am using eval() so I can basically write the array address on the fly, eg:
for($i=0;$i<count($cats);$i++){
$tree_address .= '[\''.$ids[$i].'\']';
}
$add_to_array = '$category_tree'.$tree_address.'[\''.$row->catID.'\'] = array();';
eval($add_to_array);
It 'ain pretty but it works and it takes a lot fewer lines of code tha I expected!