3v4l.org

run code in 300+ PHP versions simultaneously
<?php function permStacker($array,$length){ $stack=[[]]; // declare intitial empty element for($x=0; $x<$length; ++$x){ // limit the total number of passes / max string length foreach($stack as $combination){ foreach(array_diff($array,range('a',end($combination))) as $left){ // do not include iterate letter that come earlier than rightmost letter $merge=array_merge($combination,[$left]); $stack[implode($merge)]=$merge; // keys hold desired strings, values hold subarray of combinations for iterated referencing } } } unset($stack[0]); // remove initial empty element return array_keys($stack); // return array of permutations as space delimited strings } $permutations=permStacker(range('a','h'),4); echo 'Total Permutations: ',sizeof($permutations),"\n"; var_export($permutations);
Output for 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Warning: range(): Argument #2 ($end) must be a single byte string if argument #1 ($start) is a single byte string, argument #1 ($start) converted to 0 in /in/eW4A6 on line 6 Warning: range(): Argument #2 ($end) must be a single byte string if argument #1 ($start) is a single byte string, argument #1 ($start) converted to 0 in /in/eW4A6 on line 6 Warning: range(): Argument #2 ($end) must be a single byte string if argument #1 ($start) is a single byte string, argument #1 ($start) converted to 0 in /in/eW4A6 on line 6 Warning: range(): Argument #2 ($end) must be a single byte string if argument #1 ($start) is a single byte string, argument #1 ($start) converted to 0 in /in/eW4A6 on line 6 Total Permutations: 162 array ( 0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', 4 => 'e', 5 => 'f', 6 => 'g', 7 => 'h', 8 => 'ab', 9 => 'ac', 10 => 'ad', 11 => 'ae', 12 => 'af', 13 => 'ag', 14 => 'ah', 15 => 'bc', 16 => 'bd', 17 => 'be', 18 => 'bf', 19 => 'bg', 20 => 'bh', 21 => 'cd', 22 => 'ce', 23 => 'cf', 24 => 'cg', 25 => 'ch', 26 => 'de', 27 => 'df', 28 => 'dg', 29 => 'dh', 30 => 'ef', 31 => 'eg', 32 => 'eh', 33 => 'fg', 34 => 'fh', 35 => 'gh', 36 => 'abc', 37 => 'abd', 38 => 'abe', 39 => 'abf', 40 => 'abg', 41 => 'abh', 42 => 'acd', 43 => 'ace', 44 => 'acf', 45 => 'acg', 46 => 'ach', 47 => 'ade', 48 => 'adf', 49 => 'adg', 50 => 'adh', 51 => 'aef', 52 => 'aeg', 53 => 'aeh', 54 => 'afg', 55 => 'afh', 56 => 'agh', 57 => 'bcd', 58 => 'bce', 59 => 'bcf', 60 => 'bcg', 61 => 'bch', 62 => 'bde', 63 => 'bdf', 64 => 'bdg', 65 => 'bdh', 66 => 'bef', 67 => 'beg', 68 => 'beh', 69 => 'bfg', 70 => 'bfh', 71 => 'bgh', 72 => 'cde', 73 => 'cdf', 74 => 'cdg', 75 => 'cdh', 76 => 'cef', 77 => 'ceg', 78 => 'ceh', 79 => 'cfg', 80 => 'cfh', 81 => 'cgh', 82 => 'def', 83 => 'deg', 84 => 'deh', 85 => 'dfg', 86 => 'dfh', 87 => 'dgh', 88 => 'efg', 89 => 'efh', 90 => 'egh', 91 => 'fgh', 92 => 'abcd', 93 => 'abce', 94 => 'abcf', 95 => 'abcg', 96 => 'abch', 97 => 'abde', 98 => 'abdf', 99 => 'abdg', 100 => 'abdh', 101 => 'abef', 102 => 'abeg', 103 => 'abeh', 104 => 'abfg', 105 => 'abfh', 106 => 'abgh', 107 => 'acde', 108 => 'acdf', 109 => 'acdg', 110 => 'acdh', 111 => 'acef', 112 => 'aceg', 113 => 'aceh', 114 => 'acfg', 115 => 'acfh', 116 => 'acgh', 117 => 'adef', 118 => 'adeg', 119 => 'adeh', 120 => 'adfg', 121 => 'adfh', 122 => 'adgh', 123 => 'aefg', 124 => 'aefh', 125 => 'aegh', 126 => 'afgh', 127 => 'bcde', 128 => 'bcdf', 129 => 'bcdg', 130 => 'bcdh', 131 => 'bcef', 132 => 'bceg', 133 => 'bceh', 134 => 'bcfg', 135 => 'bcfh', 136 => 'bcgh', 137 => 'bdef', 138 => 'bdeg', 139 => 'bdeh', 140 => 'bdfg', 141 => 'bdfh', 142 => 'bdgh', 143 => 'befg', 144 => 'befh', 145 => 'begh', 146 => 'bfgh', 147 => 'cdef', 148 => 'cdeg', 149 => 'cdeh', 150 => 'cdfg', 151 => 'cdfh', 152 => 'cdgh', 153 => 'cefg', 154 => 'cefh', 155 => 'cegh', 156 => 'cfgh', 157 => 'defg', 158 => 'defh', 159 => 'degh', 160 => 'dfgh', 161 => 'efgh', )
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 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.33, 8.2.0 - 8.2.29
Total Permutations: 162 array ( 0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', 4 => 'e', 5 => 'f', 6 => 'g', 7 => 'h', 8 => 'ab', 9 => 'ac', 10 => 'ad', 11 => 'ae', 12 => 'af', 13 => 'ag', 14 => 'ah', 15 => 'bc', 16 => 'bd', 17 => 'be', 18 => 'bf', 19 => 'bg', 20 => 'bh', 21 => 'cd', 22 => 'ce', 23 => 'cf', 24 => 'cg', 25 => 'ch', 26 => 'de', 27 => 'df', 28 => 'dg', 29 => 'dh', 30 => 'ef', 31 => 'eg', 32 => 'eh', 33 => 'fg', 34 => 'fh', 35 => 'gh', 36 => 'abc', 37 => 'abd', 38 => 'abe', 39 => 'abf', 40 => 'abg', 41 => 'abh', 42 => 'acd', 43 => 'ace', 44 => 'acf', 45 => 'acg', 46 => 'ach', 47 => 'ade', 48 => 'adf', 49 => 'adg', 50 => 'adh', 51 => 'aef', 52 => 'aeg', 53 => 'aeh', 54 => 'afg', 55 => 'afh', 56 => 'agh', 57 => 'bcd', 58 => 'bce', 59 => 'bcf', 60 => 'bcg', 61 => 'bch', 62 => 'bde', 63 => 'bdf', 64 => 'bdg', 65 => 'bdh', 66 => 'bef', 67 => 'beg', 68 => 'beh', 69 => 'bfg', 70 => 'bfh', 71 => 'bgh', 72 => 'cde', 73 => 'cdf', 74 => 'cdg', 75 => 'cdh', 76 => 'cef', 77 => 'ceg', 78 => 'ceh', 79 => 'cfg', 80 => 'cfh', 81 => 'cgh', 82 => 'def', 83 => 'deg', 84 => 'deh', 85 => 'dfg', 86 => 'dfh', 87 => 'dgh', 88 => 'efg', 89 => 'efh', 90 => 'egh', 91 => 'fgh', 92 => 'abcd', 93 => 'abce', 94 => 'abcf', 95 => 'abcg', 96 => 'abch', 97 => 'abde', 98 => 'abdf', 99 => 'abdg', 100 => 'abdh', 101 => 'abef', 102 => 'abeg', 103 => 'abeh', 104 => 'abfg', 105 => 'abfh', 106 => 'abgh', 107 => 'acde', 108 => 'acdf', 109 => 'acdg', 110 => 'acdh', 111 => 'acef', 112 => 'aceg', 113 => 'aceh', 114 => 'acfg', 115 => 'acfh', 116 => 'acgh', 117 => 'adef', 118 => 'adeg', 119 => 'adeh', 120 => 'adfg', 121 => 'adfh', 122 => 'adgh', 123 => 'aefg', 124 => 'aefh', 125 => 'aegh', 126 => 'afgh', 127 => 'bcde', 128 => 'bcdf', 129 => 'bcdg', 130 => 'bcdh', 131 => 'bcef', 132 => 'bceg', 133 => 'bceh', 134 => 'bcfg', 135 => 'bcfh', 136 => 'bcgh', 137 => 'bdef', 138 => 'bdeg', 139 => 'bdeh', 140 => 'bdfg', 141 => 'bdfh', 142 => 'bdgh', 143 => 'befg', 144 => 'befh', 145 => 'begh', 146 => 'bfgh', 147 => 'cdef', 148 => 'cdeg', 149 => 'cdeh', 150 => 'cdfg', 151 => 'cdfh', 152 => 'cdgh', 153 => 'cefg', 154 => 'cefh', 155 => 'cegh', 156 => 'cfgh', 157 => 'defg', 158 => 'defh', 159 => 'degh', 160 => 'dfgh', 161 => 'efgh', )

preferences:
171.36 ms | 423 KiB | 5 Q