> 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:
<?php
$array_in['apples'] = array("red","granny smith");
$array_in['fruits'] = array("apples","oranges","pears");
$array_in['oranges'] = array('mandarin');
$array_in['red'] = array("big red","small red");
$array_in['oranges'] = array('mandarins');
#STEP 1: find highest in hierarchy to make stems
#a Make $highest_keywords_array contain all mentioned keywords.
$highest_keywords_array = array_keys($array_in);
#first make a list of all values
$values_array[]='';
function val_array (&$arrayname)
{global $values_array;
foreach
(array_values($arrayname) as $key)
{if
(!is_array($key))$values_array[] = $key;
val_array ($key);
}
}
val_array(&$array_in );
ECHO '<b>Original keywords:</b>'; foreach ($highest_keywords_array as $key)
{ echo $key .' - ';}
ECHO '<br><b>Original values:</b>'; foreach ($values_array as $val) { echo
$val .' - ';};
#b Now look for every key if there is a 'higher' keyword, if so delete it
from this array.
$position=0;
foreach ($highest_keywords_array as $key)
{if (in_array($key,$values_array))
{array_splice($highest_keywords_array,$position,1);$position--;}
$position++;
}
ECHO '<br><b>Stripped to highest level keyword(s):</b>';
foreach ($highest_keywords_array as $key) { echo $key .' ';}
$arrayname=$highest_keywords_array ;
function GLUE (&$arrayname)
{ global $array_in;
$position=0;
foreach ($arrayname as $key)
{#echo '<br>^'.$key.'--
'.$array_in[$key].'['.isset($array_in[$key]).']';
if (isset($array_in[$key])){
$arrayname[$key] = $array_in[$key];
GLUE
($arrayname[$key]);
$position++;
}
}
}
GLUE(&$arrayname );
# done, now show it!
echo "<table bgcolor=green
border=1><tr><td><b>Level</></td><td><b>value</></td></tr>";
function showarray (&$arrayname,$level)
{ foreach ($arrayname as $key)
{
if (!is_array($key))
{echo
"<tr><td>$level</td><td>";
for
($i=0;$i<$level;$i++) {echo
' ';}
echo
'-'.$key.'</td></tr>';
}
if
(is_array($arrayname[$key])) showarray
($arrayname[$key],$level+1);
}
}
showarray(&$arrayname,0 );
echo '</table>';
?>
--------------------------------------------------------------------
-- 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]