3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ 1,2,3,4 ]; $expected = [ '1-2-3-4', '2-1-3-4', '3-1-2-4', '1-3-2-4', '2-3-1-4', '3-2-1-4', '4-2-1-3', '2-4-1-3', '1-4-2-3', '4-1-2-3', '2-1-4-3', '1-2-4-3', '1-3-4-2', '3-1-4-2', '4-1-3-2', '1-4-3-2', '3-4-1-2', '4-3-1-2', '4-3-2-1', '3-4-2-1', '2-4-3-1', '4-2-3-1', '3-2-4-1', '2-3-4-1', ]; function mixture( $size, array &$collection ) { $permutations = []; $offset = $size - 1; if ( 1 === $size ) { $permutations[] = implode( '-', $collection ); return $permutations; } for ( $i = 0; $i < $offset; $i++ ) { $permutations = array_merge( $permutations, mixture( $offset, $collection ) ); $j = ( 0 == $size % 2 ) ? $i : 0; $tmp_el = $collection[ $offset ]; $collection[ $offset ] = $collection[ $j ]; $collection[ $j ] = $tmp_el; } $permutations = array_merge( $permutations, mixture( $offset, $collection ) ); return $permutations; } print_r($permutations = mixture( count($arr), $arr )); if ($expected == $permutations) { echo 'PASS'.PHP_EOL; } else { echo 'FAIL'.PHP_EOL; echo 'missing: '.PHP_EOL; print_r(array_diff($expected, array_unique($permutations))); }
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.29, 8.2.0 - 8.2.20, 8.3.0 - 8.3.8
Array ( [0] => 1-2-3-4 [1] => 2-1-3-4 [2] => 3-1-2-4 [3] => 1-3-2-4 [4] => 2-3-1-4 [5] => 3-2-1-4 [6] => 4-2-1-3 [7] => 2-4-1-3 [8] => 1-4-2-3 [9] => 4-1-2-3 [10] => 2-1-4-3 [11] => 1-2-4-3 [12] => 1-3-4-2 [13] => 3-1-4-2 [14] => 4-1-3-2 [15] => 1-4-3-2 [16] => 3-4-1-2 [17] => 4-3-1-2 [18] => 4-3-2-1 [19] => 3-4-2-1 [20] => 2-4-3-1 [21] => 4-2-3-1 [22] => 3-2-4-1 [23] => 2-3-4-1 ) PASS

preferences:
176.95 ms | 404 KiB | 222 Q