Hi,
Soll es eine dreidimensionale Permutation werden, also jede Anordnungsmöglichkeit jeder Menge mit jeder Anordnungsmöglichkeit der beiden anderen kombiniert werden?
Nein nur eine Richtung und Dimension,
array
(
array('a1', 'a2', 'a3'),
array('b1', 'b2'),
array('c1', 'c2', 'c3'),
array('d1', 'd2', 'd3', 'd4' )
)
Permutation
a1 b1 c1 d1
a1 b1 c1 d2
a1 b1 c1 d3
a1 b1 c1 d4
a1 b1 c2 d1
a1 b1 c2 d2
a1 b1 c2 d3
a1 b1 c2 d4
a1 b1 c3 d1
a1 b1 c3 d2
a1 b1 c3 d3
a1 b1 c3 d4
a1 b2 c1 d1
a1 b2 c1 d2
a1 b2 c1 d3
a1 b2 c1 d4
a1 b2 c2 d1
a1 b2 c2 d2
a1 b2 c2 d3
a1 b2 c2 d4
a1 b2 c3 d1
a1 b2 c3 d2
a1 b2 c3 d3
a1 b2 c3 d4
Quelltext für Beispiel:
function showCombinations($string, $traits, $i)
{
if ($i >= count($traits))
{
echo '<b style="color:red" >'.trim($string) . "</b>\n";
}
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) {
echo "<hr>";
}
// 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('a1', 'a2', 'a3'),
array('b1', 'b2'),
array('c1', 'c2', 'c3'),
array('d1', 'd2', 'd3', 'd4' )
)
;
$size = count($set) - 1;
$perm = range(0, $size);
$j = 0;
//do {
//echo print_r($perm);
foreach ($perm as $i) {
$perms[$j][] = $set[$i];
}
//}
while ($perm = pc_next_permutation($perm, $size));
foreach ($perms as $p) {
//print_r($p);
//print_r($perms);
showCombinations('', $p, 0);
}
Grüße Simone