3v4l.org

run code in 300+ PHP versions simultaneously
<?php $in = array('e1','e2','e3','e4'); //$new =[]; $count = count($in); Foreach($in as $key => $val){ $new[] = $val; // add first value // loop through in to greate the long incrementing string For($i=$key; $i<=$count-$key;$i++){ if($key != 0){ $new[] = implode("-",array_slice($in,$key,$i)); }else{ if($i - $key>1) $new[] = implode("-",array_slice($in,$key,$i)); } } // all but second to last except if iteration has come to far if($count-2-$key >1) $new[] = Implode("-",Array_slice($in,$key,$count-2)). "-". $in[$count-1]; // $key (skip one) next one. except if iteration has come to far If($count-2-$key >1) $new[] = $in[$key] . "-" . $in[$key+2]; // $key (skip one) rest of array except if iteration has come to far if($count-2-$key > 1) $new[] = $in[$key] ."-". Implode("-",Array_slice($in,$key+2)); // $key and last item, except if iteration has come to far if($count-1 - $key >1) $new[] = $in[$key] ."-". $in[$count-1]; } $new = array_unique($new); // remove any duplicates that may have been created $result = array( 0 => 'e1', 1 => 'e1-e2', 2 => 'e1-e2-e3', 3 => 'e1-e2-e3-e4', 4 => 'e1-e2-e4', 5 => 'e1-e3', 6 => 'e1-e3-e4', 7 => 'e1-e4', 8 => 'e2', 9 => 'e2-e3', 10 => 'e2-e3-e4', 11 => 'e2-e4', 12 => 'e3', 13 => 'e3-e4', 14 => 'e4' ); var_dump(array_diff($new,$result)); // compare arrays if there is any difference Var_dump($new, $result); // output them

preferences:
35.6 ms | 402 KiB | 5 Q