<?php $e = array( array('a1', 'a2'), array('b1', 'b2', 'b3', 'b4'), array('c1', 'c2'), ); $N = count($e); $r = array(); $n = array_fill(0, $N, 1); for ($i = 1; $i < $N; $i++) for ($j = $i; $j < $N; $j++) { $cnt = count($e[$j]); $n[$i - 1] *= $cnt; } $h = $n[0] * count($e[0]); for ($i = 0; $i < $h; $i++) for ($j = 0; $j < $N; $j++) { $m = intval($i / $n[$j]) % (count($e[$j])); $r[$i][$j] = $e[$j][$m]; } echo "n:", PHP_EOL, implode(" -> ", $n), PHP_EOL; echo "res:", PHP_EOL; foreach ($r as $v) echo implode(" -> ", $v), PHP_EOL; /* output: -------------- n: 8 -> 2 -> 1 -------------- res: a1 -> b1 -> c1 a1 -> b1 -> c2 a1 -> b2 -> c1 a1 -> b2 -> c2 a1 -> b3 -> c1 a1 -> b3 -> c2 a1 -> b4 -> c1 a1 -> b4 -> c2 a2 -> b1 -> c1 a2 -> b1 -> c2 a2 -> b2 -> c1 a2 -> b2 -> c2 a2 -> b3 -> c1 a2 -> b3 -> c2 a2 -> b4 -> c1 a2 -> b4 -> c2 -------------- */