PHP Portal » PHP Forum » Entwicklung » Code-Schnipsel » alle Kombinationen ermitteln » Antworten

Antwort erstellen

Titel
Formatierung
b i u
Beitrag  
Datei anhängen max: 2M
Optionen







Spamschutz Eingabeformat HH:MM

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
21
22
23
24
25
26
27
28
$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]; } // foreach( $counter as $k=>$v ) { $alle[$i][] = $data[$k][$v]; } // ++$counter[0]; } // print_r( $alle );