php中没有重复的有序组合?
我有以下数组: 数组(“一”、“二”、“三”、“四”) 我需要一个函数,可以输出数组元素的所有有序组合,而不需要重复。输出应该是php中没有重复的有序组合?,php,combinations,Php,Combinations,我有以下数组: 数组(“一”、“二”、“三”、“四”) 我需要一个函数,可以输出数组元素的所有有序组合,而不需要重复。输出应该是 one one two one two three one two three four two two three two three four three three four four 输出不应包括: one one one two four two one three four two three 等等 你们中有人已经实现了这种alghoritm吗?这将只
one
one two
one two three
one two three four
two
two three
two three four
three
three four
four
输出不应包括:
one one
one two four
two one three
four two three
等等
你们中有人已经实现了这种alghoritm吗?这将只是两个嵌套循环:
你试过什么吗?这似乎很简单。我在网上搜索了各种解决方案,但我发现的都是有/没有重复的排列,不适合我的需要
function permutation(array $arr)
{
while($ele=array_shift($arr))
{
$x=$ele;
echo $x."\n";
foreach($arr as $rest)
{
$x.=" $rest";
echo $x."\n";
}
}
}
permutation(array("one","two","three","four"));