fork download
  1. <?php
  2.  
  3.  
  4. $array = [1, 2, 3];
  5.  
  6. function getFactorial($array){
  7. $factorial = 1;
  8. for($n = 1; $n<=count($array); $n++){
  9. $factorial *= $n;
  10. }
  11. return $factorial;
  12. }
  13.  
  14. $factorial = getFactorial($array);
  15.  
  16. function getCombination($array){
  17. $combination = [];
  18. for($n = 0; $n<=(count($array) - 1); $n++){
  19. if(($n+1) > (count($array) - 1)){
  20. $combination[] = $array[0];
  21. } else {
  22. $combination[] = $array[$n+1];
  23. }
  24. }
  25. return $combination;
  26. }
  27.  
  28. function Main($factorial, $array){
  29. $halfFactorial = $factorial/2;
  30. $combination = getCombination($array);
  31. while($factorial > 0){
  32. $combination = getCombination($combination);
  33. if($factorial <= $halfFactorial){
  34. krsort($combination);
  35. }
  36. print_r($combination);
  37. $factorial--;
  38. }
  39. }
  40.  
  41. Main($factorial, $array);
Success #stdin #stdout 0.02s 25816KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 3
    [1] => 1
    [2] => 2
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Array
(
    [0] => 2
    [1] => 3
    [2] => 1
)
Array
(
    [2] => 2
    [1] => 1
    [0] => 3
)
Array
(
    [2] => 3
    [1] => 2
    [0] => 1
)
Array
(
    [2] => 1
    [1] => 3
    [0] => 2
)