0

Lets say i have an array:

array('a', 'b', 'c', 'd', 'e', 'f');

I need to find all combinations with 5 letters:

array(array('a', 'b', 'c', 'd', 'e'), array('a', 'b', 'c', 'd', 'f'), array('a', 'b', 'c', 'e', 'f'), ...)

I cant have duplicate values:

array('a', 'a', 'c', 'c', 'e');
Eligijus
  • 634
  • 1
  • 6
  • 15
  • 1
    What do you have try so far? – Med May 20 '15 at 15:07
  • 1
    possible duplicate of [PHP Find All (somewhat) Unique Combinations of an Array](http://stackoverflow.com/questions/16310553/php-find-all-somewhat-unique-combinations-of-an-array) – vib May 20 '15 at 15:07
  • I have never done this before, and honestly I don't really have the time to write and test this. However I feel like if you shift your thought from `find all combinations` to `generate all combinations` you should be able to do it with a 4 or 5 embedded `for loop` system. – Newd May 20 '15 at 15:10
  • Not sure if this is an exact duplicate of a previous question, as there is the criterion on not being able to match combinations with duplicate values – David Wyly May 20 '15 at 15:13
  • @DavidWyly We don't know is the duplication criteria is on keys or on values... – Random May 20 '15 at 15:14
  • @Eligijus : if your input is `array('a', 'a', 'b', 'c', 'd', 'e', 'f')`, do you accept an output like `array('a', 'a', 'c', 'd', 'e')` ? (since a is twice in input, it can be twice in output) – Random May 20 '15 at 15:15
  • 1
    If duplicate values aren't permitted in the input array, do an array_unique() on the input array before generating the list of unique combinations as per the possible duplicate linked above – Mark Baker May 20 '15 at 15:18

1 Answers1

0

You could use a combination of array_unique and count.

$array = array('a', 'b', 'c', 'd', 'e', 'f');
$unique = array_unique($array);
if (count($unique) >= 5) {
    // yay... found one
}

Depending on what you're doing you could make that a function and use it in a loop.

Then once you know how many unique characters you have, there will be a mathematical way to work out how many possible combinations you can have.

diggersworld
  • 12,770
  • 24
  • 84
  • 119