fork download
  1. <?php
  2. function recombine($right, $left = array())
  3. {
  4. echo implode(' ', $left).PHP_EOL; // trace
  5. while($value = array_shift($right)) // walk through $right like queue (mutable!)
  6. recombine($right, array_merge(array($value), $left)); // go deep but put value to left
  7. };
  8.  
  9. recombine(array(1, 2, 3, 4));
  10.  
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
1
2 1
3 2 1
4 3 2 1
4 2 1
3 1
4 3 1
4 1
2
3 2
4 3 2
4 2
3
4 3
4