putting vectors into matrix

I have a variable number of vextors of variable length. I needt to put them into a rectangular matrix with as many columns as there are vectors and rows which contain all combinations of vector cell entries. For example, given the three vectors:
i need a matrix with 3 columns and 12 rows:
a c f
a c g
a d f
a d g
a e f
a e g
b c f
etc to…
b e g
Thanks for any help

Loops are your friend:

$m1 = array("a", "b");
$m2 = array("c", "d", "e");
$m3 = array("f", "g");

$output = array();
$x = 0;

for ($i = 0; $i < count($m1); $i++) {
  $first = $m1[$i];
  for ($j = 0; $j < count($m2); $j++) {
    $second = $m2[$i];
    for ($k = 0; $k < count($m3); $k++) {
      $third = $m3[$i];
      $output[$x++] = array($first, $second, $third);

Thanks Zyppora, but the number of vectors is variable, so you can’t use $first, $second etc. However, I’ve solved it, painstakingly, by looking at the number of times each cell is repeated in a column, and the number of cycles of cell entries.

Sponsor our Newsletter | Privacy Policy | Terms of Service