-1

我有以下数组:

数组(“一”、“二”、“三”、“四”);

我需要一个函数来输出数组元素的所有有序组合而不重复。输出应该是

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

等等 ...

你们中有人已经实施过这种算法吗?

4

1 回答 1

2

这只是两个嵌套循环:

在线演示

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"));
于 2013-09-25T09:28:56.910 回答