Hi,
try this recursive solution:
<?PHP
// recursive function to calculate "tree" of
// combinations.
function combine($arrStr,$val,&$arrResult) {
if (!empty($arrStr)) {
// character array still contains some characters
// get and remove first one
$char = array_shift($arrStr);
// add character to the actual value $val
$newval = $val.$char;
// add new value to result array. Use length of
// new value as index so we can display the
// values from shortest to longest easily
$arrResult[strlen($newval)][] = $newval;
// call function with new value (follow tree to the left)
combine($arrStr,$newval,$arrResult);
// call function with old value (follow tree to the right)
combine($arrStr,$val,$arrResult);
}
}
// test string
$str = "a b c d";
// create array
$arrStr = explode(" ",$str);
// call recursive function
combine($arrStr,'',$arrResult);
// print result
foreach($arrResult as $arr) {
foreach($arr as $value) {
echo "$value<br>\n";
}
}
?>
Thomas