3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = ['a', 'b', 'c', 'd','e','f','g']; function combinations($array, $k) { if ($k === 0) { return [[]]; } if (count($array) === $k) { return [$array]; } if (count($array) < $k) { return []; } $results = []; list($firstElem, $restArray) = [array_slice($array, 0, 1), array_slice($array, 1)]; foreach (combinations($restArray, $k - 1) as $combination) { $results[] = array_merge($firstElem, $combination); } foreach (combinations($restArray, $k) as $combination) { $results[] = $combination; } return $results; } $combinations = array_map('implode', combinations($array, 2)); print_r($combinations); ?>

preferences:
40.34 ms | 407 KiB | 5 Q