3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function combinations($arr) { $combinations = array_fill(0, count($arr)+1, array()); $combinations[0] = array(''); for ($i = 0, $n = count($arr); $i < $n; ++$i) { for ($l = $n-$i; $l > 0; --$l) { $combinations[$l][] = implode('', array_slice($arr, $i, $l)); } } return $combinations; } $arr = array('d', 'f', 'w', 's'); var_dump(combinations($arr));
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(5) { [0]=> array(1) { [0]=> string(0) "" } [1]=> array(4) { [0]=> string(1) "d" [1]=> string(1) "f" [2]=> string(1) "w" [3]=> string(1) "s" } [2]=> array(3) { [0]=> string(2) "df" [1]=> string(2) "fw" [2]=> string(2) "ws" } [3]=> array(2) { [0]=> string(3) "dfw" [1]=> string(3) "fws" } [4]=> array(1) { [0]=> string(4) "dfws" } }