Jump to content

Recommended Posts

problem:

 

variable number of arrays each with variable number of elements.

 

need to be able to put them together in every combination possible.

 

example:

 

array 1 (red, blue, green)

array 2 (small, med, large)

array 3 (car, truck, van)

 

some of the 162 possible combos are:

 

red small truck

red truck small

small red truck

small truck red

 

etc.

 

i have no idea how to set up the loops to accomplish this, especially with a variable number of arrays.

i assume it'll need to use a multidimensional array, but i was wondering if anybody knows how to set the loops up.

 

so array[0][0] = red

array [0][1] = blue

...

array [1][0] = small

 

but how to make it go through them all correctly?

 

thanks!

Link to comment
https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/
Share on other sites

i think i have the theory:

 

i have the code that will spit out all combinations if the arrays are in a set position:

 

so if arrays are:

 

A

B

C

 

and each array A,B,C has elements like i gave in the example before, it will spit out:

 

red small truck

red small van

red small car

...

 

but 'color' is always in the first position. so i need to write an algorithm that will swap array positions. in

this case it would have 6 iterations (3*2*1)

 

A  B  B  C  C  A

A  C B  A  C

C  C  A  A  B  B

 

and of course with 4 arrays there would be 24 iterations.

 

anybody know how to do this? thanks.

the length of each array given is not an issue at this point. but yes, it has to work for any number of arrays. i'm working on it. talk to you in a year :).

I think I have a way. I'm going to try it and update this topic when I have made any progress. :)

 

Please don't expect an update by today. :P

i found a solution...not mine.

 

the pc_next_permutation function gives all permutations of a variable number of arrays.

the showCombinations function outputs the variations of the arrays elements at each permutation.

 

i'd be interested to see your solution though. thanks.

 

function showCombinations($string, $traits, $i)

{

    if ($i >= count($traits))

        echo trim($string) . "<br/>";

    else

    {

        foreach ($traits[$i] as $trait)

            showCombinations("$string $trait", $traits, $i + 1);

    }

}

 

 

function pc_next_permutation($p, $size) {

    // slide down the array looking for where we're smaller than the nextguy

    for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { }

    // if this doesn't occur, we've finished our permutations

    // the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)

      if ($i == -1) { return false; }

      // slide down the array looking for a bigger number than what we foundbefore

      for ($j = $size; $p[$j] <= $p[$i]; --$j) { }

      // swap them

      $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;

      // now reverse the elements in between by swapping the ends

      for (++$i, $j = $size; $i < $j; ++$i, --$j) {

            $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;

      }

      return $p;

}

$set = array

(

    array('red', 'blue', 'green'),

    array('small', 'medium'),

    array('car', 'truck', 'van')

);

$size = count($set) - 1;

$perm = range(0, $size);

$j = 0;

do {

      foreach ($perm as $i) { $perms[$j][] = $set[$i]; }

} while ($perm = pc_next_permutation($perm, $size) and ++$j);

foreach ($perms as $p) {

showCombinations('', $p, 0);

 

}

try

<?php
function comb($a){
$out = array();
if (count($a) == 1) {
	$x = array_shift($a);
	foreach ($x as $v) $out[] = array($v);
	return $out;
}
foreach ($a as $k => $v){
	$b = $a;
	unset($b[$k]);
	$x = comb($b);
	foreach ($v as $v1){
		foreach ($x As $v2) 
		$out[] = array_merge(array($v1), $v2);
	}
}
return $out;
}


$test = array(array('red', 'blue', 'green'),array('small', 'med', 'large'),array('car', 'truck', 'van'));

$x = comb($test);
print_r($x);
?>

  • 3 years later...

Hi there. I need some help. If i have M arrays with N elements in each array (for example: array1 = {D1,D2,...Dn}, array2 ={O1,O2,..On},...arrayM={... }), i want to generate only ONE possible combination with elements from each array, assuming that any elements can repeat, but only N times. This is like representation of a WAVELENGTH SELECTIVE optical network, in which one DEMUX (D1) has N wavelengths, each of them can be sent to exactly ONE O1 switch, and then to one MUX M. One combination of those elements represents one lightpath, and different lightpath are passing through same elements, but only N lightpaths in one elements. If anybody can help me, I would be thankful. Best regards!!

  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.