3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $words=array("A", "B", "C", "D"); $combos=array(); getAllWordCombos($words,$combos, array()); print_r($combos); function getAllWordCombos($words, &$combos, $partial) { if (count($words)==1) $combos[]=array_merge($partial,$words); else { foreach ($words as $i=>$first) { $cwords=array_slice($words,0); array_splice($cwords,$i,1); getAllWordCombos($cwords, $combos, array_merge($partial,array($first))); } } }
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Array ( [0] => Array ( [0] => A [1] => B [2] => C [3] => D ) [1] => Array ( [0] => A [1] => B [2] => D [3] => C ) [2] => Array ( [0] => A [1] => C [2] => B [3] => D ) [3] => Array ( [0] => A [1] => C [2] => D [3] => B ) [4] => Array ( [0] => A [1] => D [2] => B [3] => C ) [5] => Array ( [0] => A [1] => D [2] => C [3] => B ) [6] => Array ( [0] => B [1] => A [2] => C [3] => D ) [7] => Array ( [0] => B [1] => A [2] => D [3] => C ) [8] => Array ( [0] => B [1] => C [2] => A [3] => D ) [9] => Array ( [0] => B [1] => C [2] => D [3] => A ) [10] => Array ( [0] => B [1] => D [2] => A [3] => C ) [11] => Array ( [0] => B [1] => D [2] => C [3] => A ) [12] => Array ( [0] => C [1] => A [2] => B [3] => D ) [13] => Array ( [0] => C [1] => A [2] => D [3] => B ) [14] => Array ( [0] => C [1] => B [2] => A [3] => D ) [15] => Array ( [0] => C [1] => B [2] => D [3] => A ) [16] => Array ( [0] => C [1] => D [2] => A [3] => B ) [17] => Array ( [0] => C [1] => D [2] => B [3] => A ) [18] => Array ( [0] => D [1] => A [2] => B [3] => C ) [19] => Array ( [0] => D [1] => A [2] => C [3] => B ) [20] => Array ( [0] => D [1] => B [2] => A [3] => C ) [21] => Array ( [0] => D [1] => B [2] => C [3] => A ) [22] => Array ( [0] => D [1] => C [2] => A [3] => B ) [23] => Array ( [0] => D [1] => C [2] => B [3] => A ) )