-2

This is a expanded question from "How can I list all possible combinations?". For the last question it showed all permutations instead of all combinations.

$num_array2 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

foreach($num_array2 AS $value1) {
    foreach($num_array2 AS $value2) {
        if($value2 == $value1) {
            continue;
        }
        foreach($num_array2 AS $value3) {
            if($value3 == $value1 || $value3 == $value2) {
                continue;
            }
            foreach($num_array2 AS $value4) {
                if($value4 == $value1 || $value4 == $value2 || $value4 == $value3) {
                    continue;
                }
                foreach($num_array2 AS $value5) {
                    if($value5 == $value1 || $value5 == $value2 || $value5 == $value3 || $value5 == $value4) {
                        continue;
                    }
                    foreach($num_array2 AS $value6) {
                        if($value6 == $value1 || $value6 == $value2 || $value6 == $value3 || $value6 == $value4 || $value6 == $value5) {
                            continue;
                        }
                        echo "$value1, $value2, $value3, $value4, $value5, $value6 \n<br />";
                    }
                }
            }
        }
    }
}

The Result will be:

1, 2, 3, 4, 5, 6 
1, 2, 3, 4, 5, 7 
1, 2, 3, 4, 5, 8 
1, 2, 3, 4, 5, 9 
1, 2, 3, 4, 5, 10 
1, 2, 3, 4, 6, 5 
1, 2, 3, 4, 6, 7 
1, 2, 3, 4, 6, 8 
1, 2, 3, 4, 6, 9 
1, 2, 3, 4, 6, 10 

As you can see "1, 2, 3, 4, 5, 6" and "1, 2, 3, 4, 6, 5" was repeated (although it wasn't the same order). I don't want the results like "1, 2, 3, 4, 6, 5" to be shown. How should I modify the code?

Community
  • 1
  • 1
AkiEru
  • 780
  • 1
  • 9
  • 34
  • 4
    Jesus.. where is that from? What if you'll have 50 elements in array? – Alma Do Nov 11 '13 at 09:24
  • It takes a long time to run a code that have 50 elements so I reduced to 10 elements first to test the code. – AkiEru Nov 11 '13 at 09:28
  • 2
    possible duplicate of [Permutations - all possible sets of numbers](http://stackoverflow.com/questions/5506888/permutations-all-possible-sets-of-numbers) – Vlad Preda Nov 11 '13 at 09:29
  • I would like to list combinations instead of permutations. – AkiEru Nov 11 '13 at 09:35

1 Answers1

1

Ignoring your code problems with escalation, your problem resides in the fact that you are only constraining your outputs to not to have repeated digits. However you need a stronger constraint: Not to have the same selection for two different outputs,

The easier way to get that is to track which array position you have already chosen: Consider a bit array as long as num_array2 , this array representns a selection so if you have taken a position, i of num_array2 then your bit array should contain a 1 in that position.

Then you can store your bit array in a set "selection signatures" and for each possible output check if its selection signature is already in that set.

This is a rather simple and silly solution but I think it can help you to understand your problem.