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); ?>
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.1
Array ( [0] => ab [1] => ac [2] => ad [3] => ae [4] => af [5] => ag [6] => bc [7] => bd [8] => be [9] => bf [10] => bg [11] => cd [12] => ce [13] => cf [14] => cg [15] => de [16] => df [17] => dg [18] => ef [19] => eg [20] => fg )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
120.58 ms | 407 KiB | 5 Q