wrgill999 Posted August 27, 2008 Share Posted August 27, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/ Share on other sites More sharing options...
Ken2k7 Posted August 27, 2008 Share Posted August 27, 2008 How many arrays are you going to use? Also, this may NOT be OOP-related. Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-626645 Share on other sites More sharing options...
wrgill999 Posted August 27, 2008 Author Share Posted August 27, 2008 that's the problem for me. it will be a variable number of arrays. 1 - 5 most likely. Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-626683 Share on other sites More sharing options...
Ken2k7 Posted August 27, 2008 Share Posted August 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-626692 Share on other sites More sharing options...
wrgill999 Posted August 27, 2008 Author Share Posted August 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-626696 Share on other sites More sharing options...
wrgill999 Posted August 27, 2008 Author Share Posted August 27, 2008 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 B 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. Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-627035 Share on other sites More sharing options...
Ken2k7 Posted August 27, 2008 Share Posted August 27, 2008 But it's not that easy. I have to iterate depending on the number of arrays given AND the length of each array given. Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-627079 Share on other sites More sharing options...
wrgill999 Posted August 27, 2008 Author Share Posted August 27, 2008 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 . Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-627140 Share on other sites More sharing options...
Ken2k7 Posted August 27, 2008 Share Posted August 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-627283 Share on other sites More sharing options...
wrgill999 Posted August 27, 2008 Author Share Posted August 27, 2008 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); } Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-627375 Share on other sites More sharing options...
sasa Posted August 28, 2008 Share Posted August 28, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-627731 Share on other sites More sharing options...
mdzanko Posted March 10, 2012 Share Posted March 10, 2012 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!! Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-1325835 Share on other sites More sharing options...
1cefreak Posted October 29, 2013 Share Posted October 29, 2013 (edited) 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; Edited October 29, 2013 by 1cefreak Quote Link to comment https://forums.phpfreaks.com/topic/121501-multi-dimensional-array-combinations/#findComment-1456079 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.