fork(2) download
  1. <?php
  2.  
  3. function permute($items, $perms = array(), $result = array())
  4. {
  5. if (empty($items))
  6. {
  7. $result[] = join('', $perms);
  8. }
  9. else
  10. {
  11. for ($i = 0; $i < count($items); ++$i)
  12. {
  13. $newitems = $items;
  14. $newperms = $perms;
  15. $foo = implode(array_splice($newitems, $i, 1));
  16. array_unshift($newperms, $foo);
  17. $result = permute($newitems, $newperms, $result);
  18. }
  19. }
  20. return $result;
  21. }
  22. $bar = permute(array("A", 'B', 'C'));
  23.  
  24. var_dump($bar);
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
array(6) {
  [0]=>
  string(3) "CBA"
  [1]=>
  string(3) "BCA"
  [2]=>
  string(3) "CAB"
  [3]=>
  string(3) "ACB"
  [4]=>
  string(3) "BAC"
  [5]=>
  string(3) "ABC"
}