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('a', 'b', 'c', 'd', 'e', 'f'); 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(7) { [0]=> array(1) { [0]=> string(0) "" } [1]=> array(6) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" [4]=> string(1) "e" [5]=> string(1) "f" } [2]=> array(5) { [0]=> string(2) "ab" [1]=> string(2) "bc" [2]=> string(2) "cd" [3]=> string(2) "de" [4]=> string(2) "ef" } [3]=> array(4) { [0]=> string(3) "abc" [1]=> string(3) "bcd" [2]=> string(3) "cde" [3]=> string(3) "def" } [4]=> array(3) { [0]=> string(4) "abcd" [1]=> string(4) "bcde" [2]=> string(4) "cdef" } [5]=> array(2) { [0]=> string(5) "abcde" [1]=> string(5) "bcdef" } [6]=> array(1) { [0]=> string(6) "abcdef" } }