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('a', 'b', 'c', 'd'))); ?>
Output for 4.3.0 - 7.1.0
Array ( [0] => aaaa [1] => aaab [2] => aaac [3] => aaad [4] => aaba [5] => aabb [6] => aabc [7] => aabd [8] => aaca [9] => aacb [10] => aacc [11] => aacd [12] => aada [13] => aadb [14] => aadc [15] => aadd [16] => abaa [17] => abab [18] => abac [19] => abad [20] => abba [21] => abbb [22] => abbc [23] => abbd [24] => abca [25] => abcb [26] => abcc [27] => abcd [28] => abda [29] => abdb [30] => abdc [31] => abdd [32] => acaa [33] => acab [34] => acac [35] => acad [36] => acba [37] => acbb [38] => acbc [39] => acbd [40] => acca [41] => accb [42] => accc [43] => accd [44] => acda [45] => acdb [46] => acdc [47] => acdd [48] => adaa [49] => adab [50] => adac [51] => adad [52] => adba [53] => adbb [54] => adbc [55] => adbd [56] => adca [57] => adcb [58] => adcc [59] => adcd [60] => adda [61] => addb [62] => addc [63] => addd [64] => baaa [65] => baab [66] => baac [67] => baad [68] => baba [69] => babb [70] => babc [71] => babd [72] => baca [73] => bacb [74] => bacc [75] => bacd [76] => bada [77] => badb [78] => badc [79] => badd [80] => bbaa [81] => bbab [82] => bbac [83] => bbad [84] => bbba [85] => bbbb [86] => bbbc [87] => bbbd [88] => bbca [89] => bbcb [90] => bbcc [91] => bbcd [92] => bbda [93] => bbdb [94] => bbdc [95] => bbdd [96] => bcaa [97] => bcab [98] => bcac [99] => bcad [100] => bcba [101] => bcbb [102] => bcbc [103] => bcbd [104] => bcca [105] => bccb [106] => bccc [107] => bccd [108] => bcda [109] => bcdb [110] => bcdc [111] => bcdd [112] => bdaa [113] => bdab [114] => bdac [115] => bdad [116] => bdba [117] => bdbb [118] => bdbc [119] => bdbd [120] => bdca [121] => bdcb [122] => bdcc [123] => bdcd [124] => bdda [125] => bddb [126] => bddc [127] => bddd [128] => caaa [129] => caab [130] => caac [131] => caad [132] => caba [133] => cabb [134] => cabc [135] => cabd [136] => caca [137] => cacb [138] => cacc [139] => cacd [140] => cada [141] => cadb [142] => cadc [143] => cadd [144] => cbaa [145] => cbab [146] => cbac [147] => cbad [148] => cbba [149] => cbbb [150] => cbbc [151] => cbbd [152] => cbca [153] => cbcb [154] => cbcc [155] => cbcd [156] => cbda [157] => cbdb [158] => cbdc [159] => cbdd [160] => ccaa [161] => ccab [162] => ccac [163] => ccad [164] => ccba [165] => ccbb [166] => ccbc [167] => ccbd [168] => ccca [169] => cccb [170] => cccc [171] => cccd [172] => ccda [173] => ccdb [174] => ccdc [175] => ccdd [176] => cdaa [177] => cdab [178] => cdac [179] => cdad [180] => cdba [181] => cdbb [182] => cdbc [183] => cdbd [184] => cdca [185] => cdcb [186] => cdcc [187] => cdcd [188] => cdda [189] => cddb [190] => cddc [191] => cddd [192] => daaa [193] => daab [194] => daac [195] => daad [196] => daba [197] => dabb [198] => dabc [199] => dabd [200] => daca [201] => dacb [202] => dacc [203] => dacd [204] => dada [205] => dadb [206] => dadc [207] => dadd [208] => dbaa [209] => dbab [210] => dbac [211] => dbad [212] => dbba [213] => dbbb [214] => dbbc [215] => dbbd [216] => dbca [217] => dbcb [218] => dbcc [219] => dbcd [220] => dbda [221] => dbdb [222] => dbdc [223] => dbdd [224] => dcaa [225] => dcab [226] => dcac [227] => dcad [228] => dcba [229] => dcbb [230] => dcbc [231] => dcbd [232] => dcca [233] => dccb [234] => dccc [235] => dccd [236] => dcda [237] => dcdb [238] => dcdc [239] => dcdd [240] => ddaa [241] => ddab [242] => ddac [243] => ddad [244] => ddba [245] => ddbb [246] => ddbc [247] => ddbd [248] => ddca [249] => ddcb [250] => ddcc [251] => ddcd [252] => ddda [253] => dddb [254] => dddc [255] => dddd )