|
Posted by Oli Filth on 09/27/71 11:43
kanka wrote:
> Here is my problem,
>
> I have a string like $numbers="12345";
>
> i wanna find out all possible (unique) combinations of each subs.
> e.g. 1 - 2 - 3 - 4 - 5 - 12 - 13 - 14 - 15 - 23 - 24 - ..... - 234 -
> 245 - .... - 12345
> but not 11 - 22 - 21 - 32 - 321....
>
> here is the function that i wrote not returns 13 - 14 - 15 or 24 - 25
>
> $numbers="12345";
> $total=strlen($numbers);
> for ($j=0; $j< $total; $j++) {
> for ($k=0; $k< ($total-$j); $k++) {
> $c[]=substr($numbers, $k, $j+1);
> }
> }
This sound like you want *permutations*, not *combinations*.
You can create permutations of an array with a recursive function,
something like:
function getPermutations($values)
{
$perms = array(null);
foreach ($values as $key => $v)
{
$temp = $values;
unset($temp[$key]);
$newPerms = getPermutations($temp);
foreach ($newPerms as $perm)
{
$perms[] = ($v . $perm);
}
}
return $perms;
}
This is inefficient, but it works.
--
Oli
Navigation:
[Reply to this message]
|