3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $a = array(array(1, 2), array(3, 4), array(5)); function get_combinations($arrays) { $result = array(array()); $count = count($arrays); $first_array = $arrays[0]; if($count > 2) { $array_merg = get_combinations(array_slice($arrays, 1)); } else { $array_merg = $arrays[1]; } $tmp = array(); foreach($first_array as $arele1) { //print_r($arele1); foreach($array_merg as $arele2) { print_r($arele2); if(is_array($arele2)) { echo 'asds'; $tmp[] = array_merge($arele1,$arele2); } else { $tmp[] = array($arele1,$arele2); } print_r($tmp); } } print_r($tmp); return $tmp; } var_dump(get_combinations($a));
Output for 5.0.0 - 7.2.0
5Array ( [0] => Array ( [0] => 3 [1] => 5 ) ) 5Array ( [0] => Array ( [0] => 3 [1] => 5 ) [1] => Array ( [0] => 4 [1] => 5 ) ) Array ( [0] => Array ( [0] => 3 [1] => 5 ) [1] => Array ( [0] => 4 [1] => 5 ) ) Array ( [0] => 3 [1] => 5 ) asds Warning: array_merge(): Argument #1 is not an array in /in/jUvdv on line 22 Array ( [0] => ) Array ( [0] => 4 [1] => 5 ) asds Warning: array_merge(): Argument #1 is not an array in /in/jUvdv on line 22 Array ( [0] => [1] => ) Array ( [0] => 3 [1] => 5 ) asds Warning: array_merge(): Argument #1 is not an array in /in/jUvdv on line 22 Array ( [0] => [1] => [2] => ) Array ( [0] => 4 [1] => 5 ) asds Warning: array_merge(): Argument #1 is not an array in /in/jUvdv on line 22 Array ( [0] => [1] => [2] => [3] => ) Array ( [0] => [1] => [2] => [3] => ) array(4) { [0]=> NULL [1]=> NULL [2]=> NULL [3]=> NULL }
Output for 4.3.0 - 4.4.9
5Array ( [0] => Array ( [0] => 3 [1] => 5 ) ) 5Array ( [0] => Array ( [0] => 3 [1] => 5 ) [1] => Array ( [0] => 4 [1] => 5 ) ) Array ( [0] => Array ( [0] => 3 [1] => 5 ) [1] => Array ( [0] => 4 [1] => 5 ) ) Array ( [0] => 3 [1] => 5 ) asdsArray ( [0] => Array ( [0] => 1 [1] => 3 [2] => 5 ) ) Array ( [0] => 4 [1] => 5 ) asdsArray ( [0] => Array ( [0] => 1 [1] => 3 [2] => 5 ) [1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) ) Array ( [0] => 3 [1] => 5 ) asdsArray ( [0] => Array ( [0] => 1 [1] => 3 [2] => 5 ) [1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) [2] => Array ( [0] => 2 [1] => 3 [2] => 5 ) ) Array ( [0] => 4 [1] => 5 ) asdsArray ( [0] => Array ( [0] => 1 [1] => 3 [2] => 5 ) [1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) [2] => Array ( [0] => 2 [1] => 3 [2] => 5 ) [3] => Array ( [0] => 2 [1] => 4 [2] => 5 ) ) Array ( [0] => Array ( [0] => 1 [1] => 3 [2] => 5 ) [1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) [2] => Array ( [0] => 2 [1] => 3 [2] => 5 ) [3] => Array ( [0] => 2 [1] => 4 [2] => 5 ) ) array(4) { [0]=> array(3) { [0]=> int(1) [1]=> int(3) [2]=> int(5) } [1]=> array(3) { [0]=> int(1) [1]=> int(4) [2]=> int(5) } [2]=> array(3) { [0]=> int(2) [1]=> int(3) [2]=> int(5) } [3]=> array(3) { [0]=> int(2) [1]=> int(4) [2]=> int(5) } }