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', 'c', 'd'], ['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 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Array ( [0] => a,a,a,a [1] => b,a,a,a [2] => c,a,a,a [3] => d,a,a,a [4] => a,b,a,a [5] => b,b,a,a [6] => c,b,a,a [7] => d,b,a,a [8] => a,c,a,a [9] => b,c,a,a [10] => c,c,a,a [11] => d,c,a,a [12] => a,d,a,a [13] => b,d,a,a [14] => c,d,a,a [15] => d,d,a,a [16] => a,a,b,a [17] => b,a,b,a [18] => c,a,b,a [19] => d,a,b,a [20] => a,b,b,a [21] => b,b,b,a [22] => c,b,b,a [23] => d,b,b,a [24] => a,c,b,a [25] => b,c,b,a [26] => c,c,b,a [27] => d,c,b,a [28] => a,d,b,a [29] => b,d,b,a [30] => c,d,b,a [31] => d,d,b,a [32] => a,a,c,a [33] => b,a,c,a [34] => c,a,c,a [35] => d,a,c,a [36] => a,b,c,a [37] => b,b,c,a [38] => c,b,c,a [39] => d,b,c,a [40] => a,c,c,a [41] => b,c,c,a [42] => c,c,c,a [43] => d,c,c,a [44] => a,d,c,a [45] => b,d,c,a [46] => c,d,c,a [47] => d,d,c,a [48] => a,a,d,a [49] => b,a,d,a [50] => c,a,d,a [51] => d,a,d,a [52] => a,b,d,a [53] => b,b,d,a [54] => c,b,d,a [55] => d,b,d,a [56] => a,c,d,a [57] => b,c,d,a [58] => c,c,d,a [59] => d,c,d,a [60] => a,d,d,a [61] => b,d,d,a [62] => c,d,d,a [63] => d,d,d,a [64] => a,a,a,b [65] => b,a,a,b [66] => c,a,a,b [67] => d,a,a,b [68] => a,b,a,b [69] => b,b,a,b [70] => c,b,a,b [71] => d,b,a,b [72] => a,c,a,b [73] => b,c,a,b [74] => c,c,a,b [75] => d,c,a,b [76] => a,d,a,b [77] => b,d,a,b [78] => c,d,a,b [79] => d,d,a,b [80] => a,a,b,b [81] => b,a,b,b [82] => c,a,b,b [83] => d,a,b,b [84] => a,b,b,b [85] => b,b,b,b [86] => c,b,b,b [87] => d,b,b,b [88] => a,c,b,b [89] => b,c,b,b [90] => c,c,b,b [91] => d,c,b,b [92] => a,d,b,b [93] => b,d,b,b [94] => c,d,b,b [95] => d,d,b,b [96] => a,a,c,b [97] => b,a,c,b [98] => c,a,c,b [99] => d,a,c,b [100] => a,b,c,b [101] => b,b,c,b [102] => c,b,c,b [103] => d,b,c,b [104] => a,c,c,b [105] => b,c,c,b [106] => c,c,c,b [107] => d,c,c,b [108] => a,d,c,b [109] => b,d,c,b [110] => c,d,c,b [111] => d,d,c,b [112] => a,a,d,b [113] => b,a,d,b [114] => c,a,d,b [115] => d,a,d,b [116] => a,b,d,b [117] => b,b,d,b [118] => c,b,d,b [119] => d,b,d,b [120] => a,c,d,b [121] => b,c,d,b [122] => c,c,d,b [123] => d,c,d,b [124] => a,d,d,b [125] => b,d,d,b [126] => c,d,d,b [127] => d,d,d,b [128] => a,a,a,c [129] => b,a,a,c [130] => c,a,a,c [131] => d,a,a,c [132] => a,b,a,c [133] => b,b,a,c [134] => c,b,a,c [135] => d,b,a,c [136] => a,c,a,c [137] => b,c,a,c [138] => c,c,a,c [139] => d,c,a,c [140] => a,d,a,c [141] => b,d,a,c [142] => c,d,a,c [143] => d,d,a,c [144] => a,a,b,c [145] => b,a,b,c [146] => c,a,b,c [147] => d,a,b,c [148] => a,b,b,c [149] => b,b,b,c [150] => c,b,b,c [151] => d,b,b,c [152] => a,c,b,c [153] => b,c,b,c [154] => c,c,b,c [155] => d,c,b,c [156] => a,d,b,c [157] => b,d,b,c [158] => c,d,b,c [159] => d,d,b,c [160] => a,a,c,c [161] => b,a,c,c [162] => c,a,c,c [163] => d,a,c,c [164] => a,b,c,c [165] => b,b,c,c [166] => c,b,c,c [167] => d,b,c,c [168] => a,c,c,c [169] => b,c,c,c [170] => c,c,c,c [171] => d,c,c,c [172] => a,d,c,c [173] => b,d,c,c [174] => c,d,c,c [175] => d,d,c,c [176] => a,a,d,c [177] => b,a,d,c [178] => c,a,d,c [179] => d,a,d,c [180] => a,b,d,c [181] => b,b,d,c [182] => c,b,d,c [183] => d,b,d,c [184] => a,c,d,c [185] => b,c,d,c [186] => c,c,d,c [187] => d,c,d,c [188] => a,d,d,c [189] => b,d,d,c [190] => c,d,d,c [191] => d,d,d,c )
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[', expecting ')' in /in/g43UC 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/g43UC 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/g43UC 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/g43UC on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/g43UC on line 2
Process exited with code 255.