3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function comb($arr) { $combs = array_fill(0, count($arr)+1, array()); for ($i = 0, $n = count($arr); $i < $n; ++$i) { for ($j = $n-$i; $j > 0; --$j) { $combs[$j][] = implode(', ', array_slice($arr, $i, $j)); } } return $combs; } $arr = array('a', 'b', 'c', 'd', 'e', 'f'); print_r(comb($arr));
Output for 4.3.0 - 7.1.0
Array ( [0] => Array ( ) [1] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) [2] => Array ( [0] => a, b [1] => b, c [2] => c, d [3] => d, e [4] => e, f ) [3] => Array ( [0] => a, b, c [1] => b, c, d [2] => c, d, e [3] => d, e, f ) [4] => Array ( [0] => a, b, c, d [1] => b, c, d, e [2] => c, d, e, f ) [5] => Array ( [0] => a, b, c, d, e [1] => b, c, d, e, f ) [6] => Array ( [0] => a, b, c, d, e, f ) )