Jump to content

wrgill999

New Members
  • Posts

    6
  • Joined

  • Last visited

    Never

Everything posted by wrgill999

  1. 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); }
  2. 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 .
  3. 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.
  4. 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.
  5. that's the problem for me. it will be a variable number of arrays. 1 - 5 most likely.
  6. 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!
×
×
  • 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.