Jump to content

Archived

This topic is now archived and is closed to further replies.

wrgill999

Multi Dimensional Array combinations

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!

Share this post


Link to post
Share on other sites

How many arrays are you going to use?

 

Also, this may NOT be OOP-related.

Share this post


Link to post
Share on other sites

that's the problem for me. it will be a variable number of arrays. 1 - 5 most likely.

The problem for me is the swap.

Share this post


Link to post
Share on other sites

actually, yes, you are right. i think there will need to be some resorting of the arrays based on number of elements and then go through the loop(s) again. my head hurts. thanks.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

But it's not that easy. I have to iterate depending on the number of arrays given AND the length of each array given.

Share this post


Link to post
Share on other sites

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 :).

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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);

 

}

Share this post


Link to post
Share on other sites

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);
?>

Share this post


Link to post
Share on other sites

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!!

Share this post


Link to post
Share on other sites

First Sasa thank you a lot. It really helped me. mdzanko just add break after before

}
return $out;

So your code should be

        }
        break;
}
return $out;

Share this post


Link to post
Share on other sites

×
×
  • 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.