RE: [PHP] Function to Build Trees
Antwoord naar: <[EMAIL PROTECTED]> Van:"Dan Harrington" <[EMAIL PROTECTED]> Onderwerp: RE: [PHP] Function to Build Trees > Or you could just buy from http://www.plumbdesign.com/ > or write a PHP SDK for them. > :-) sorry? what does that horrible coldfusion site have to do with this? Chris -- C.Hayes Droevendaal 35 6708 PB Wageningen the Netherlands -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Function to Build Trees
Or you could just buy from http://www.plumbdesign.com/ or write a PHP SDK for them. :-) > -Original Message- > From: Chris Hayes [mailto:[EMAIL PROTECTED]] > Sent: Monday, August 20, 2001 4:15 AM > To: [EMAIL PROTECTED] > Subject: Re: [PHP] Function to Build Trees > > > > Onderwerp: [PHP] Function to Build Trees > > A most interesting question! > > > > Given a multidimensional array with words as keys, I want to match > > the values of each array with the keys of the array and build a new > > array that reflects these relationships as a sort of tree. So, in > > the list below, "fruit", "apples", "oranges", "red" are the keys and > > in the "fruit" array are the values "apples" and "oranges", so I want > > the function to create a new array that looks like: > > > > ORIGINAL ARRAY: > > $array['fruits'] = array("apples","oranges","pears"); > > $array['apples'] = array("red","granny smith"); > > $array['oranges'] = array('mandarin'); > > $array['red'] = array("big red","small red"); > > > > NEW ARRAY: > > $array['fruits'] = array(apples,oranges,pears); > > $array['fruits']['apples'] = array("red","granny smith"); > > $array['fruits']['oranges'] = array('mandarin'); > > At first peek i fear endless loops. But never mind about those :-). Well not > never, i think you MUST test new data on the loop danger. > What may cause loops? >$array['fruits'] = array("apples","oranges","pears"); >$array['apples'] = array("red","granny smith","fruit"); > May cause an endless array fruit-apple-fruit-apple- ad infinitum. > > > > And then I want it further to figure out that "red", now in the array > > found at: $array['fruits']['apples'] matches a key in the original > > array so the final result looks like: > > > > FINAL ARRAY: > > $array['fruits'] = array(apples,oranges,pears); > > $array['fruits']['oranges'] = array('mandarin'); > > $array['fruits']['apples'] = array("red","granny smith"); > > $array['fruits']['apples']['red'] = array("big red","small red"); > > > > So, you see how it builds a little tree, right? > > I think the thing is to make a step by step description of what should be > done. > Write the logic down with this example in a way that can be translated into > array handling functions (walk through, search, add). > > I think there are several ways to approach it. > > Mine would be probably. > > STEP 1: find highest in hierarchy to make stems > a Make $highest_keywords_array contain all mentioned keywords. > b Now look for every key if there is a 'higher' keyword, if so delete it > from this array. > c You should in your example end up with $highest_keywords_array containing > only 'fruit'. > > STEP 2: build on branches and twigs > Now go through the initial array several times and glue on branches that go > under the highest keywords, then twigs, twiglets, twigletlets... > Make every word $highest_keywords_array a 1st level result array item >Walk through 1st level keys, > For every key look in initial array for the values > Stick these values in the result array: array[thiskey]=[value] > > Walk through 2nd level > > Ad 1b: > For every word in array see if it is in the array at a 'value location'. No > idea how to do it - ask the group. > > Ad 2: > I don't know how to make this function go through all existing values. I am > not going to break my early morning brain on it as i a not sure you will use > this approach. > Correction. Some questions are too tempting so i still tried. > > STEP 1 results in: > $arrayname=array(keys at 1st level, i.e. fruit); > STEP 2 > function GLUE ($arrayname) > {global $arrayname; > for every KEY in $arrayname [at >this level] > { $arrayname[KEY] = initial array[key] >
Re: [PHP] Function to Build Trees
> Onderwerp: [PHP] Function to Build Trees A most interesting question! > Given a multidimensional array with words as keys, I want to match > the values of each array with the keys of the array and build a new > array that reflects these relationships as a sort of tree. So, in > the list below, "fruit", "apples", "oranges", "red" are the keys and > in the "fruit" array are the values "apples" and "oranges", so I want > the function to create a new array that looks like: > > ORIGINAL ARRAY: > $array['fruits'] = array("apples","oranges","pears"); > $array['apples'] = array("red","granny smith"); > $array['oranges'] = array('mandarin'); > $array['red'] = array("big red","small red"); > > NEW ARRAY: > $array['fruits'] = array(apples,oranges,pears); > $array['fruits']['apples'] = array("red","granny smith"); > $array['fruits']['oranges'] = array('mandarin'); At first peek i fear endless loops. But never mind about those :-). Well not never, i think you MUST test new data on the loop danger. What may cause loops? $array['fruits'] = array("apples","oranges","pears"); $array['apples'] = array("red","granny smith","fruit"); May cause an endless array fruit-apple-fruit-apple- ad infinitum. > And then I want it further to figure out that "red", now in the array > found at: $array['fruits']['apples'] matches a key in the original > array so the final result looks like: > > FINAL ARRAY: > $array['fruits'] = array(apples,oranges,pears); > $array['fruits']['oranges'] = array('mandarin'); > $array['fruits']['apples'] = array("red","granny smith"); > $array['fruits']['apples']['red'] = array("big red","small red"); > > So, you see how it builds a little tree, right? I think the thing is to make a step by step description of what should be done. Write the logic down with this example in a way that can be translated into array handling functions (walk through, search, add). I think there are several ways to approach it. Mine would be probably. STEP 1: find highest in hierarchy to make stems a Make $highest_keywords_array contain all mentioned keywords. b Now look for every key if there is a 'higher' keyword, if so delete it from this array. c You should in your example end up with $highest_keywords_array containing only 'fruit'. STEP 2: build on branches and twigs Now go through the initial array several times and glue on branches that go under the highest keywords, then twigs, twiglets, twigletlets... Make every word $highest_keywords_array a 1st level result array item Walk through 1st level keys, For every key look in initial array for the values Stick these values in the result array: array[thiskey]=[value] Walk through 2nd level Ad 1b: For every word in array see if it is in the array at a 'value location'. No idea how to do it - ask the group. Ad 2: I don't know how to make this function go through all existing values. I am not going to break my early morning brain on it as i a not sure you will use this approach. Correction. Some questions are too tempting so i still tried. STEP 1 results in: $arrayname=array(keys at 1st level, i.e. fruit); STEP 2 function GLUE ($arrayname) {global $arrayname; for every KEY in $arrayname [at this level] { $arrayname[KEY] = initial array[key] GLUE ($arrayname[KEY]) } } GLUE($array_with_first_level_keys) This should do the reciprocal thing as efficient as possible. Think about strawberry (red) (small,big) apple (red) (small,big) when making this leveled function (or maybe reciprocal). This system should leave no orphans, as orphans should have been at the highest hierarchy. hope this helps (let me know!) Chris - PS shit i couldn't help it: Original keywords:';
[PHP] Function to Build Trees
Hello everyone, I've been tearing my hair out trying to write a function in PHP that will accomplish the following: Given a multidimensional array with words as keys, I want to match the values of each array with the keys of the array and build a new array that reflects these relationships as a sort of tree. So, in the list below, "fruit", "apples", "oranges", "red" are the keys and in the "fruit" array are the values "apples" and "oranges", so I want the function to create a new array that looks like: ORIGINAL ARRAY: $array['fruits'] = array("apples","oranges","pears"); $array['apples'] = array("red","granny smith"); $array['oranges'] = array('mandarin'); $array['red'] = array("big red","small red"); NEW ARRAY: $array['fruits'] = array(apples,oranges,pears); $array['fruits']['apples'] = array("red","granny smith"); $array['fruits']['oranges'] = array('mandarin'); And then I want it further to figure out that "red", now in the array found at: $array['fruits']['apples'] matches a key in the original array so the final result looks like: FINAL ARRAY: $array['fruits'] = array(apples,oranges,pears); $array['fruits']['oranges'] = array('mandarin'); $array['fruits']['apples'] = array("red","granny smith"); $array['fruits']['apples']['red'] = array("big red","small red"); So, you see how it builds a little tree, right? AND, I'd like this function to be able to handle any number (probably less that 30, but whatever) of initial array elements in an unpredictable order and the final tree could have up to five or six levels. You know? Is this impossible, or am I just going about the end result wrong?? It seemed like such a simple thing three days ago, but now I am about to cry uncle. Anyway, please write if you have any thoughts. Best, Sondra Russell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]