25.10.2009 12:46 von TBT
alle Kombinationen ermitteln 

da ich gerade vor so einen Problem stand, ein kleines Snippet wie man die möglichen Kombinationen
bei einer unbekannten Anzahl von Attributen und Werten ermittelt.
Das Snippet bezieht sich dabei auf die Attribute eines Shirts, von dem es mehrere Farben / Größen / Aufdrucke gibt,
und ich alle möglichen Kombinationen der Attribute haben möchte.
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$kombizahl = 1; $data = array( array( 'rot', 'gelb', 'grün'), array( 'xxl', 'xl', 'l' ), array( 'bild1', 'bild2', 'bild3' ) ); foreach( $data as $row ) { $kombizahl *= count( $row ); } $counter = array_fill( 0, $anzahl, 0 ); // aktuelle Position in jedem Attribut $alle = array(); // alle Kombinationen // alle Kombis durchlaufen for( $i=0; $i<$kombizahl; ++$i ) { $c = 0; // Counter zum hochzählen // while( $counter[$c] == count( $data[$c] ) && isset( $counter[$c+1] ) ) { $counter[$c] = 0; ++$c; ++$counter[$c]; } //
für die ungekürzte Fassung bitte einloggen
Zitat

Werbung

Schnellantwort