3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $array = array('a', 'b', 'c'); function depth_picker($arr, $temp_string, &$collect) { if ($temp_string != "") $collect []= $temp_string; for ($i=0; $i<sizeof($arr);$i++) { $arrcopy = $arr; $elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element if (sizeof($arrcopy) > 0) { depth_picker($arrcopy, $temp_string ." " . $elem[0], $collect); } else { $collect []= $temp_string. " " . $elem[0]; } } } $collect = array(); depth_picker($array, "", $collect); print_r($collect);
based on tk9Du
Output for 4.3.0 - 7.2.0
Array ( [0] => a [1] => a b [2] => a b c [3] => a c [4] => a c b [5] => b [6] => b a [7] => b a c [8] => b c [9] => b c a [10] => c [11] => c a [12] => c a b [13] => c b [14] => c b a )