3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function everyCombination($array) { $arrayCount = count($array); $maxCombinations = pow($arrayCount, $arrayCount); $returnArray = array(); $conversionArray = array(); foreach ($array as $key => $value) { $conversionArray[base_convert($key, 10, $arrayCount)] = $value; } for ($i = 0; $i < $maxCombinations; $i++) { $combination = base_convert($i, 10, $arrayCount); $combination = str_pad($combination, $arrayCount, "0", STR_PAD_LEFT); $returnArray[] = strtr($combination, $conversionArray); } return $returnArray; } print_r(everyCombination(array(1,2,3,4))); ?>
Output for 4.3.0 - 7.1.0
Array ( [0] => 1111 [1] => 1112 [2] => 1113 [3] => 1114 [4] => 1121 [5] => 1122 [6] => 1123 [7] => 1124 [8] => 1131 [9] => 1132 [10] => 1133 [11] => 1134 [12] => 1141 [13] => 1142 [14] => 1143 [15] => 1144 [16] => 1211 [17] => 1212 [18] => 1213 [19] => 1214 [20] => 1221 [21] => 1222 [22] => 1223 [23] => 1224 [24] => 1231 [25] => 1232 [26] => 1233 [27] => 1234 [28] => 1241 [29] => 1242 [30] => 1243 [31] => 1244 [32] => 1311 [33] => 1312 [34] => 1313 [35] => 1314 [36] => 1321 [37] => 1322 [38] => 1323 [39] => 1324 [40] => 1331 [41] => 1332 [42] => 1333 [43] => 1334 [44] => 1341 [45] => 1342 [46] => 1343 [47] => 1344 [48] => 1411 [49] => 1412 [50] => 1413 [51] => 1414 [52] => 1421 [53] => 1422 [54] => 1423 [55] => 1424 [56] => 1431 [57] => 1432 [58] => 1433 [59] => 1434 [60] => 1441 [61] => 1442 [62] => 1443 [63] => 1444 [64] => 2111 [65] => 2112 [66] => 2113 [67] => 2114 [68] => 2121 [69] => 2122 [70] => 2123 [71] => 2124 [72] => 2131 [73] => 2132 [74] => 2133 [75] => 2134 [76] => 2141 [77] => 2142 [78] => 2143 [79] => 2144 [80] => 2211 [81] => 2212 [82] => 2213 [83] => 2214 [84] => 2221 [85] => 2222 [86] => 2223 [87] => 2224 [88] => 2231 [89] => 2232 [90] => 2233 [91] => 2234 [92] => 2241 [93] => 2242 [94] => 2243 [95] => 2244 [96] => 2311 [97] => 2312 [98] => 2313 [99] => 2314 [100] => 2321 [101] => 2322 [102] => 2323 [103] => 2324 [104] => 2331 [105] => 2332 [106] => 2333 [107] => 2334 [108] => 2341 [109] => 2342 [110] => 2343 [111] => 2344 [112] => 2411 [113] => 2412 [114] => 2413 [115] => 2414 [116] => 2421 [117] => 2422 [118] => 2423 [119] => 2424 [120] => 2431 [121] => 2432 [122] => 2433 [123] => 2434 [124] => 2441 [125] => 2442 [126] => 2443 [127] => 2444 [128] => 3111 [129] => 3112 [130] => 3113 [131] => 3114 [132] => 3121 [133] => 3122 [134] => 3123 [135] => 3124 [136] => 3131 [137] => 3132 [138] => 3133 [139] => 3134 [140] => 3141 [141] => 3142 [142] => 3143 [143] => 3144 [144] => 3211 [145] => 3212 [146] => 3213 [147] => 3214 [148] => 3221 [149] => 3222 [150] => 3223 [151] => 3224 [152] => 3231 [153] => 3232 [154] => 3233 [155] => 3234 [156] => 3241 [157] => 3242 [158] => 3243 [159] => 3244 [160] => 3311 [161] => 3312 [162] => 3313 [163] => 3314 [164] => 3321 [165] => 3322 [166] => 3323 [167] => 3324 [168] => 3331 [169] => 3332 [170] => 3333 [171] => 3334 [172] => 3341 [173] => 3342 [174] => 3343 [175] => 3344 [176] => 3411 [177] => 3412 [178] => 3413 [179] => 3414 [180] => 3421 [181] => 3422 [182] => 3423 [183] => 3424 [184] => 3431 [185] => 3432 [186] => 3433 [187] => 3434 [188] => 3441 [189] => 3442 [190] => 3443 [191] => 3444 [192] => 4111 [193] => 4112 [194] => 4113 [195] => 4114 [196] => 4121 [197] => 4122 [198] => 4123 [199] => 4124 [200] => 4131 [201] => 4132 [202] => 4133 [203] => 4134 [204] => 4141 [205] => 4142 [206] => 4143 [207] => 4144 [208] => 4211 [209] => 4212 [210] => 4213 [211] => 4214 [212] => 4221 [213] => 4222 [214] => 4223 [215] => 4224 [216] => 4231 [217] => 4232 [218] => 4233 [219] => 4234 [220] => 4241 [221] => 4242 [222] => 4243 [223] => 4244 [224] => 4311 [225] => 4312 [226] => 4313 [227] => 4314 [228] => 4321 [229] => 4322 [230] => 4323 [231] => 4324 [232] => 4331 [233] => 4332 [234] => 4333 [235] => 4334 [236] => 4341 [237] => 4342 [238] => 4343 [239] => 4344 [240] => 4411 [241] => 4412 [242] => 4413 [243] => 4414 [244] => 4421 [245] => 4422 [246] => 4423 [247] => 4424 [248] => 4431 [249] => 4432 [250] => 4433 [251] => 4434 [252] => 4441 [253] => 4442 [254] => 4443 [255] => 4444 )