2013-09-25 36 views
0

我有以下數組:在php中沒有重複的順序組合?

Array(「one」,「two」,「three」,「four」);

我需要一個函數來輸出數組元素的所有有序組合,而不需要重複。輸出應該

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的?

+0

您是否嘗試過的東西?這看起來很簡單。 – Passerby

+0

我已經在網上搜索了各種解決方案,但是我發現的所有解決方案都有/無重複,不適合我的需要 – Scalax

回答

0

這僅僅是兩個嵌套的循環:

Online demo

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")); 
+0

謝謝,這是我尋找的解決方案 – Scalax