3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $array1 = array('A', 'B'); $array2 = array('1', '2', '3'); $num = count($array2); //The total number of possible combinations $total = pow(2, $num); $ff = array(); //Loop through each possible combination for ($i = 0; $i < $total; $i++) { //For each combination check if each bit is set for ($j = 0; $j < $num; $j++) { //Is bit $j set in $i? if (pow(2, $j) & $i) $ff[] = $ff.''.$array2[$j]; } } print_r($ff);
based on Kgos7
Output for 5.4.0 - 7.1.0
Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Notice: Array to string conversion in /in/Yt7Lc on line 18 Array ( [0] => Array1 [1] => Array2 [2] => Array1 [3] => Array2 [4] => Array3 [5] => Array1 [6] => Array3 [7] => Array2 [8] => Array3 [9] => Array1 [10] => Array2 [11] => Array3 )
Output for 4.3.0 - 5.3.29
Array ( [0] => Array1 [1] => Array2 [2] => Array1 [3] => Array2 [4] => Array3 [5] => Array1 [6] => Array3 [7] => Array2 [8] => Array3 [9] => Array1 [10] => Array2 [11] => Array3 )