3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $arrays = array(array(8,11,5,6,7), array(1,2,3), array(100,200,300), array(500,600,700)); function array_cartesian($arrays) { if(count($arrays) == 0) { return array(array()); } $a = array_shift($arrays); $c = array_cartesian($arrays); $r = array(); foreach($a as $v) { foreach($c as $p) { $r[] = array_merge(array($v), $p); } } return $r; } print_r(array_cartesian($arrays));
Output for 5.3.7 - 7.1.0
Array ( [0] => Array ( [0] => 8 [1] => 1 [2] => 100 [3] => 500 ) [1] => Array ( [0] => 8 [1] => 1 [2] => 100 [3] => 600 ) [2] => Array ( [0] => 8 [1] => 1 [2] => 100 [3] => 700 ) [3] => Array ( [0] => 8 [1] => 1 [2] => 200 [3] => 500 ) [4] => Array ( [0] => 8 [1] => 1 [2] => 200 [3] => 600 ) [5] => Array ( [0] => 8 [1] => 1 [2] => 200 [3] => 700 ) [6] => Array ( [0] => 8 [1] => 1 [2] => 300 [3] => 500 ) [7] => Array ( [0] => 8 [1] => 1 [2] => 300 [3] => 600 ) [8] => Array ( [0] => 8 [1] => 1 [2] => 300 [3] => 700 ) [9] => Array ( [0] => 8 [1] => 2 [2] => 100 [3] => 500 ) [10] => Array ( [0] => 8 [1] => 2 [2] => 100 [3] => 600 ) [11] => Array ( [0] => 8 [1] => 2 [2] => 100 [3] => 700 ) [12] => Array ( [0] => 8 [1] => 2 [2] => 200 [3] => 500 ) [13] => Array ( [0] => 8 [1] => 2 [2] => 200 [3] => 600 ) [14] => Array ( [0] => 8 [1] => 2 [2] => 200 [3] => 700 ) [15] => Array ( [0] => 8 [1] => 2 [2] => 300 [3] => 500 ) [16] => Array ( [0] => 8 [1] => 2 [2] => 300 [3] => 600 ) [17] => Array ( [0] => 8 [1] => 2 [2] => 300 [3] => 700 ) [18] => Array ( [0] => 8 [1] => 3 [2] => 100 [3] => 500 ) [19] => Array ( [0] => 8 [1] => 3 [2] => 100 [3] => 600 ) [20] => Array ( [0] => 8 [1] => 3 [2] => 100 [3] => 700 ) [21] => Array ( [0] => 8 [1] => 3 [2] => 200 [3] => 500 ) [22] => Array ( [0] => 8 [1] => 3 [2] => 200 [3] => 600 ) [23] => Array ( [0] => 8 [1] => 3 [2] => 200 [3] => 700 ) [24] => Array ( [0] => 8 [1] => 3 [2] => 300 [3] => 500 ) [25] => Array ( [0] => 8 [1] => 3 [2] => 300 [3] => 600 ) [26] => Array ( [0] => 8 [1] => 3 [2] => 300 [3] => 700 ) [27] => Array ( [0] => 11 [1] => 1 [2] => 100 [3] => 500 ) [28] => Array ( [0] => 11 [1] => 1 [2] => 100 [3] => 600 ) [29] => Array ( [0] => 11 [1] => 1 [2] => 100 [3] => 700 ) [30] => Array ( [0] => 11 [1] => 1 [2] => 200 [3] => 500 ) [31] => Array ( [0] => 11 [1] => 1 [2] => 200 [3] => 600 ) [32] => Array ( [0] => 11 [1] => 1 [2] => 200 [3] => 700 ) [33] => Array ( [0] => 11 [1] => 1 [2] => 300 [3] => 500 ) [34] => Array ( [0] => 11 [1] => 1 [2] => 300 [3] => 600 ) [35] => Array ( [0] => 11 [1] => 1 [2] => 300 [3] => 700 ) [36] => Array ( [0] => 11 [1] => 2 [2] => 100 [3] => 500 ) [37] => Array ( [0] => 11 [1] => 2 [2] => 100 [3] => 600 ) [38] => Array ( [0] => 11 [1] => 2 [2] => 100 [3] => 700 ) [39] => Array ( [0] => 11 [1] => 2 [2] => 200 [3] => 500 ) [40] => Array ( [0] => 11 [1] => 2 [2] => 200 [3] => 600 ) [41] => Array ( [0] => 11 [1] => 2 [2] => 200 [3] => 700 ) [42] => Array ( [0] => 11 [1] => 2 [2] => 300 [3] => 500 ) [43] => Array ( [0] => 11 [1] => 2 [2] => 300 [3] => 600 ) [44] => Array ( [0] => 11 [1] => 2 [2] => 300 [3] => 700 ) [45] => Array ( [0] => 11 [1] => 3 [2] => 100 [3] => 500 ) [46] => Array ( [0] => 11 [1] => 3 [2] => 100 [3] => 600 ) [47] => Array ( [0] => 11 [1] => 3 [2] => 100 [3] => 700 ) [48] => Array ( [0] => 11 [1] => 3 [2] => 200 [3] => 500 ) [49] => Array ( [0] => 11 [1] => 3 [2] => 200 [3] => 600 ) [50] => Array ( [0] => 11 [1] => 3 [2] => 200 [3] => 700 ) [51] => Array ( [0] => 11 [1] => 3 [2] => 300 [3] => 500 ) [52] => Array ( [0] => 11 [1] => 3 [2] => 300 [3] => 600 ) [53] => Array ( [0] => 11 [1] => 3 [2] => 300 [3] => 700 ) [54] => Array ( [0] => 5 [1] => 1 [2] => 100 [3] => 500 ) [55] => Array ( [0] => 5 [1] => 1 [2] => 100 [3] => 600 ) [56] => Array ( [0] => 5 [1] => 1 [2] => 100 [3] => 700 ) [57] => Array ( [0] => 5 [1] => 1 [2] => 200 [3] => 500 ) [58] => Array ( [0] => 5 [1] => 1 [2] => 200 [3] => 600 ) [59] => Array ( [0] => 5 [1] => 1 [2] => 200 [3] => 700 ) [60] => Array ( [0] => 5 [1] => 1 [2] => 300 [3] => 500 ) [61] => Array ( [0] => 5 [1] => 1 [2] => 300 [3] => 600 ) [62] => Array ( [0] => 5 [1] => 1 [2] => 300 [3] => 700 ) [63] => Array ( [0] => 5 [1] => 2 [2] => 100 [3] => 500 ) [64] => Array ( [0] => 5 [1] => 2 [2] => 100 [3] => 600 ) [65] => Array ( [0] => 5 [1] => 2 [2] => 100 [3] => 700 ) [66] => Array ( [0] => 5 [1] => 2 [2] => 200 [3] => 500 ) [67] => Array ( [0] => 5 [1] => 2 [2] => 200 [3] => 600 ) [68] => Array ( [0] => 5 [1] => 2 [2] => 200 [3] => 700 ) [69] => Array ( [0] => 5 [1] => 2 [2] => 300 [3] => 500 ) [70] => Array ( [0] => 5 [1] => 2 [2] => 300 [3] => 600 ) [71] => Array ( [0] => 5 [1] => 2 [2] => 300 [3] => 700 ) [72] => Array ( [0] => 5 [1] => 3 [2] => 100 [3] => 500 ) [73] => Array ( [0] => 5 [1] => 3 [2] => 100 [3] => 600 ) [74] => Array ( [0] => 5 [1] => 3 [2] => 100 [3] => 700 ) [75] => Array ( [0] => 5 [1] => 3 [2] => 200 [3] => 500 ) [76] => Array ( [0] => 5 [1] => 3 [2] => 200 [3] => 600 ) [77] => Array ( [0] => 5 [1] => 3 [2] => 200 [3] => 700 ) [78] => Array ( [0] => 5 [1] => 3 [2] => 300 [3] => 500 ) [79] => Array ( [0] => 5 [1] => 3 [2] => 300 [3] => 600 ) [80] => Array ( [0] => 5 [1] => 3 [2] => 300 [3] => 700 ) [81] => Array ( [0] => 6 [1] => 1 [2] => 100 [3] => 500 ) [82] => Array ( [0] => 6 [1] => 1 [2] => 100 [3] => 600 ) [83] => Array ( [0] => 6 [1] => 1 [2] => 100 [3] => 700 ) [84] => Array ( [0] => 6 [1] => 1 [2] => 200 [3] => 500 ) [85] => Array ( [0] => 6 [1] => 1 [2] => 200 [3] => 600 ) [86] => Array ( [0] => 6 [1] => 1 [2] => 200 [3] => 700 ) [87] => Array ( [0] => 6 [1] => 1 [2] => 300 [3] => 500 ) [88] => Array ( [0] => 6 [1] => 1 [2] => 300 [3] => 600 ) [89] => Array ( [0] => 6 [1] => 1 [2] => 300 [3] => 700 ) [90] => Array ( [0] => 6 [1] => 2 [2] => 100 [3] => 500 ) [91] => Array ( [0] => 6 [1] => 2 [2] => 100 [3] => 600 ) [92] => Array ( [0] => 6 [1] => 2 [2] => 100 [3] => 700 ) [93] => Array ( [0] => 6 [1] => 2 [2] => 200 [3] => 500 ) [94] => Array ( [0] => 6 [1] => 2 [2] => 200 [3] => 600 ) [95] => Array ( [0] => 6 [1] => 2 [2] => 200 [3] => 700 ) [96] => Array ( [0] => 6 [1] => 2 [2] => 300 [3] => 500 ) [97] => Array ( [0] => 6 [1] => 2 [2] => 300 [3] => 600 ) [98] => Array ( [0] => 6 [1] => 2 [2] => 300 [3] => 700 ) [99] => Array ( [0] => 6 [1] => 3 [2] => 100 [3] => 500 ) [100] => Array ( [0] => 6 [1] => 3 [2] => 100 [3] => 600 ) [101] => Array ( [0] => 6 [1] => 3 [2] => 100 [3] => 700 ) [102] => Array ( [0] => 6 [1] => 3 [2] => 200 [3] => 500 ) [103] => Array ( [0] => 6 [1] => 3 [2] => 200 [3] => 600 ) [104] => Array ( [0] => 6 [1] => 3 [2] => 200 [3] => 700 ) [105] => Array ( [0] => 6 [1] => 3 [2] => 300 [3] => 500 ) [106] => Array ( [0] => 6 [1] => 3 [2] => 300 [3] => 600 ) [107] => Array ( [0] => 6 [1] => 3 [2] => 300 [3] => 700 ) [108] => Array ( [0] => 7 [1] => 1 [2] => 100 [3] => 500 ) [109] => Array ( [0] => 7 [1] => 1 [2] => 100 [3] => 600 ) [110] => Array ( [0] => 7 [1] => 1 [2] => 100 [3] => 700 ) [111] => Array ( [0] => 7 [1] => 1 [2] => 200 [3] => 500 ) [112] => Array ( [0] => 7 [1] => 1 [2] => 200 [3] => 600 ) [113] => Array ( [0] => 7 [1] => 1 [2] => 200 [3] => 700 ) [114] => Array ( [0] => 7 [1] => 1 [2] => 300 [3] => 500 ) [115] => Array ( [0] => 7 [1] => 1 [2] => 300 [3] => 600 ) [116] => Array ( [0] => 7 [1] => 1 [2] => 300 [3] => 700 ) [117] => Array ( [0] => 7 [1] => 2 [2] => 100 [3] => 500 ) [118] => Array ( [0] => 7 [1] => 2 [2] => 100 [3] => 600 ) [119] => Array ( [0] => 7 [1] => 2 [2] => 100 [3] => 700 ) [120] => Array ( [0] => 7 [1] => 2 [2] => 200 [3] => 500 ) [121] => Array ( [0] => 7 [1] => 2 [2] => 200 [3] => 600 ) [122] => Array ( [0] => 7 [1] => 2 [2] => 200 [3] => 700 ) [123] => Array ( [0] => 7 [1] => 2 [2] => 300 [3] => 500 ) [124] => Array ( [0] => 7 [1] => 2 [2] => 300 [3] => 600 ) [125] => Array ( [0] => 7 [1] => 2 [2] => 300 [3] => 700 ) [126] => Array ( [0] => 7 [1] => 3 [2] => 100 [3] => 500 ) [127] => Array ( [0] => 7 [1] => 3 [2] => 100 [3] => 600 ) [128] => Array ( [0] => 7 [1] => 3 [2] => 100 [3] => 700 ) [129] => Array ( [0] => 7 [1] => 3 [2] => 200 [3] => 500 ) [130] => Array ( [0] => 7 [1] => 3 [2] => 200 [3] => 600 ) [131] => Array ( [0] => 7 [1] => 3 [2] => 200 [3] => 700 ) [132] => Array ( [0] => 7 [1] => 3 [2] => 300 [3] => 500 ) [133] => Array ( [0] => 7 [1] => 3 [2] => 300 [3] => 600 ) [134] => Array ( [0] => 7 [1] => 3 [2] => 300 [3] => 700 ) )