3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function P(array $sources) { $result=array(); $cache=array(); foreach($sources as $node) { $cache=$result; $result=array(); foreach($node as $item) { if(empty($cache)) { $result[]=array($item); } else { foreach($cache as $line) { $line[]=$item; $result[]=$line; } } } } return $result; } $result=P([ ['a', 'b'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c'] ]); print_r(array_map(function($a){return implode(",",$a);},$result));
Output for 5.4.0 - 7.1.0
Array ( [0] => a,a,a,a [1] => b,a,a,a [2] => a,b,a,a [3] => b,b,a,a [4] => a,c,a,a [5] => b,c,a,a [6] => a,d,a,a [7] => b,d,a,a [8] => a,a,b,a [9] => b,a,b,a [10] => a,b,b,a [11] => b,b,b,a [12] => a,c,b,a [13] => b,c,b,a [14] => a,d,b,a [15] => b,d,b,a [16] => a,a,c,a [17] => b,a,c,a [18] => a,b,c,a [19] => b,b,c,a [20] => a,c,c,a [21] => b,c,c,a [22] => a,d,c,a [23] => b,d,c,a [24] => a,a,d,a [25] => b,a,d,a [26] => a,b,d,a [27] => b,b,d,a [28] => a,c,d,a [29] => b,c,d,a [30] => a,d,d,a [31] => b,d,d,a [32] => a,a,a,b [33] => b,a,a,b [34] => a,b,a,b [35] => b,b,a,b [36] => a,c,a,b [37] => b,c,a,b [38] => a,d,a,b [39] => b,d,a,b [40] => a,a,b,b [41] => b,a,b,b [42] => a,b,b,b [43] => b,b,b,b [44] => a,c,b,b [45] => b,c,b,b [46] => a,d,b,b [47] => b,d,b,b [48] => a,a,c,b [49] => b,a,c,b [50] => a,b,c,b [51] => b,b,c,b [52] => a,c,c,b [53] => b,c,c,b [54] => a,d,c,b [55] => b,d,c,b [56] => a,a,d,b [57] => b,a,d,b [58] => a,b,d,b [59] => b,b,d,b [60] => a,c,d,b [61] => b,c,d,b [62] => a,d,d,b [63] => b,d,d,b [64] => a,a,a,c [65] => b,a,a,c [66] => a,b,a,c [67] => b,b,a,c [68] => a,c,a,c [69] => b,c,a,c [70] => a,d,a,c [71] => b,d,a,c [72] => a,a,b,c [73] => b,a,b,c [74] => a,b,b,c [75] => b,b,b,c [76] => a,c,b,c [77] => b,c,b,c [78] => a,d,b,c [79] => b,d,b,c [80] => a,a,c,c [81] => b,a,c,c [82] => a,b,c,c [83] => b,b,c,c [84] => a,c,c,c [85] => b,c,c,c [86] => a,d,c,c [87] => b,d,c,c [88] => a,a,d,c [89] => b,a,d,c [90] => a,b,d,c [91] => b,b,d,c [92] => a,c,d,c [93] => b,c,d,c [94] => a,d,d,c [95] => b,d,d,c )
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[', expecting ')' in /in/gS3Ml on line 28
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/gS3Ml on line 2
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_ARRAY, expecting ')' in /in/gS3Ml on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_ARRAY, expecting ')' in /in/gS3Ml on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/gS3Ml on line 2
Process exited with code 255.