2012-04-05 10 views
1

Mögliche Duplizieren:
PHP take all combinationsPHP Mögliche Kombinationen 3 Arrays von 2 Werten

Ich denke, etwas in PHP zu machen, die mir alle Kombinationen von Nummernschildern zeigen. Zum Beispiel:

Sie haben 3-Boxen Sie in max 2 Werte

Wie

BOX1 BOX2 BOX3 
75 PM M5 
7S PH MS 
Z5 PN H5 
ZS RM HS 
25 RH N5 
2S RN NS 

NOT BOX1+BOX1+BOX1 
It needs to show me 
ex. 75-PM-M5 
ex. 75-PH-MS 
ex. 75-PN-MS 
ex. 75-PM-H5 
ex. 75-PH-H5 
ex. 75-PN-H5 
So, BOX1+BOX2+BOX3 

The PHP script needs to calculate all the combinations for BOX1+BOX2+BOX3 
So BOX1 value1 and value2 are ONE value not two separate values. 
In BOX2 value1 and value2 are also ONE value not two separate values and so on. 

If I want all combinations of 
BOX1'91' + BOX2'HF' + BOX3'PF' 
BOX1'74' + BOX2'RT' + BOX3'YT' 

It will calculate an amount of 2x2x2=8 combinations 

ex. 91-HF-PF 
ex. 91-HF-YT 
ex. 91-RT-PF 
ex. 91-RT-YT 
ex. 74-HF-PF 
ex. 74-HF-YT 
ex. 74-RT-PF 
ex. 74-RT-YT 

Beispiel pincode

Sie 4 Pincodes eingeben müssen füllen können Ihre apm Karte

verwenden
PIN1+PIN2+PIN3+PIN4 
1 1 1 1 
2 2 2 2 
3 3 3 3 
4 4 4 4 
5 5 5 5 
6 6 6 6 
7 7 7 7 
8 8 8 8 
9 9 9 9 
0 0 0 0 

So haben Sie insgesamt Kombination von 10x1 0x10x10 = 10.000 Kombinationen und es hat alle Kombinationen zeigen

, wenn mir jemand helfen könnte aus wäre ich dankbar

+0

in diesem Thema meine Antwort Check: http://stackoverflow.com/questions/9787051/php-take-all-combinations/9787438#9787438. Es sollte auch für dich funktionieren. –

+0

Das sieht aus, was ich brauche, aber es zeigt auch Katze Katze. Aber das will ich nicht. Es müssen nur Kombinationen von box1box2box3 nicht box1box1box1 angezeigt werden .. irgendwelche Ideen? – MOTIVECODEX

+0

SO: Kein Element kann wiederholt werden (z. B. NO A-A-B, C-C-C usw.) & A-B-C wird als verschieden von A-C-B angesehen. Sind meine Annahmen richtig? –

Antwort

4

Code:

<?php 

function combinations($arr, $n) 
{ 
    $res = array(); 

    foreach ($arr[$n] as $item) 
    { 
     if ($n==count($arr)-1) 
      $res[]=$item; 
     else 
     { 
      $combs = combinations($arr,$n+1); 

      foreach ($combs as $comb) 
      { 
       $res[] = "$item $comb"; 
      } 
     } 
    } 
    return $res; 
} 

// Your ARRAY (first array is like 'BOX1', etc -) 
// you can put as many items in each 'BOX' as you like... 
// and as many 'boxes' as you like 
$words = array(array('A','B'),array('C','D'), array('E','F')); 

$combos = combinations($words,0); // ALWAYS, call it with 0 as the last parameter 
print_r($combos); 

?> 

Ausgang:

Array 
(
    [0] => A C E 
    [1] => A C F 
    [2] => A D E 
    [3] => A D F 
    [4] => B C E 
    [5] => B C F 
    [6] => B D E 
    [7] => B D F 
) 

I GENAU denke, das ist, was Sie brauchen ... :-)

+1

Es war genau das, was ich brauchte! +1 – user1105056

Verwandte Themen