0

I need a function that will return all possible unique variations of an input array, but not repeat the array elements so [a,b,c] and [b,c,a] are the same.

I have a working function that outputs the desired result using a binary count. So for an input array of array("a", "b", "c") it will output:

Array
(
    [0] => Array
        (
            [0] => c
        )

    [1] => Array
        (
            [0] => b
        )

    [2] => Array
        (
            [0] => b
            [1] => c
        )

    [3] => Array
        (
            [0] => a
        )

    [4] => Array
        (
            [0] => a
            [1] => c
        )

    [5] => Array
        (
            [0] => a
            [1] => b
        )

    [6] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

)

However I'm finding that for arrays with 20 or more elements the code runs out of memory. I suspect because of the $binary variable

The code I'm using is:

function find_unique_combinations($arr){
    $bits = count($arr);
    $dec = 1;
    while($dec < pow(2, $bits)) {
        $binary  = str_split(str_pad(decbin($dec), $bits, '0', STR_PAD_LEFT));
        $curterm = array();
        $i = 0;
        while($i < ($bits)){
            if($binary[$i] == 1) {
                $curterm[] = $arr[$i];
            }
            $i++;
        }

        $terms[] = $curterm;
        $dec++;
    }
    return $terms;
}

The error message im getting is

Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 36 bytes)

php.ini is currently set to 512MB, I'd rather fix the code than allocate more memory if possible.

Usman
  • 5
  • 4
  • possible duplicate of:- [PHP Find All (somewhat) Unique Combinations of an Array](https://stackoverflow.com/questions/16310553/php-find-all-somewhat-unique-combinations-of-an-array) – Alive to die - Anant Oct 15 '19 at 11:46
  • Also useful:-[Finding unique array combinations with PHP (permutations)](https://dannyherran.com/2011/06/finding-unique-array-combinations-with-php-permutations/) – Alive to die - Anant Oct 15 '19 at 11:47
  • https://3v4l.org/IMOJE ....... https://www.oreilly.com/library/view/php-cookbook/1565926811/ch04s25.html – Alive to die - Anant Oct 15 '19 at 11:59
  • Hi Anant, Your suggestions also encounter a memory issue, I did search for other ways of doing this but found the same problem. `Fatal error: Allowed memory size of 536870912 bytes exhausted` – Usman Oct 15 '19 at 13:45

1 Answers1

0

So after much analysis and taking the code apart, I have come to the conclusion that the memory exhausted error will always occur, no matter how the function is coded. As you increase the number of array elements you exponentially increase the amount of possible combinations to the point of reaching the system memory limit.

I have therefore limited the input array to 15 which would produce 32,768 possible combinations. This should cover the most likely use case of this function.

Thanks @Anant_Singh for taking a look.

Usman
  • 5
  • 4