Forum Moderators: coopster

Message Too Old, No Replies

Editing / manipulating array tree structures

         

Robber

9:52 am on Jul 8, 2004 (gmt 0)

10+ Year Member



Morning all,

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

Robber

10:54 am on Jul 8, 2004 (gmt 0)

10+ Year Member



Thought I'd found a solution, PEAR::Tree looked like it might do the trick for me but it seems you need to have your source data either in a DB or in XML, mine is stored in arrays.. the hunt goes on!

coopster

2:34 pm on Jul 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You've already described what you want, have you tried writing your own using the PHP array [php.net] functions?

Robber

2:52 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



Hi Coopster,

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!

coopster

4:09 pm on Jul 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Just a note, you aren't closing your double quotes on your array keys.

Robber

4:20 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



Hi Coopster,

Thanks for checking, you nearly had me going there!

Have another look, its not actually a double quote, its ane scaped single quote followed bu another single quote.

Are there any drawbacks of using the eval() function like this that might be detrimental to my scripts?

Cheers

coopster

4:24 pm on Jul 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Oh! I wasn't trying to get you going, I honestly thought you had a syntax issue there! And no, I don't see how using eval() would have any drawbacks.