Search code examples
phparrayscombinationsmemory-limit

Allowed memory size of 268435456 bytes exhausted with array


I am trying to make a function to generate all possible combinations. This part works well so the output would be

aaa aab aac aad ...

But now i am trying to add an extension to each of the combinations so i want to add "HI" at the end like

aaaHI aabHI aacHI aadHI

Iv tried the following but im getting this error. Is there a better way to do this that what i am doing?

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 100663409 bytes)

here is my script

function sampling($chars, $size, $combinations = array()) {

    # if it's the first iteration, the first set 
    # of combinations is the same as the set of characters
    if (empty($combinations)) {
        $combinations = $chars;
    }

    # we're done if we're at size 1
    if ($size == 1) {
        return $combinations;
    }

    # initialise array to put new values in
    $new_combinations = array();

    # loop through existing combinations and character set to create strings
    foreach ($combinations as $combination) {

        foreach ($chars as $char) {
            $new_combinations[] = $combination. $char;
            $new_combinations[] = implode($new_combinations, "HI");
        }
    }

    # call same function again for the next iteration
    return sampling($chars, $size - 1, $new_combinations);

}


// example
$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');

$output = sampling($chars, 3);

foreach($output as $do) 
{ 
    echo $do."<br>";
} 

Solution

  • This is not really clear what you are going to do, but first of all, you are using implode() incorrectly. The first argument must be $glue and the second your array.

    string implode ( string $glue , array $pieces )

    Secondly, your $new_combinations array is growing progressively on each step.

    But if I understood what you were going to do, will this code work for you:

    <?php
    
    function sampling($chars, $size, $combinations = array()) {
    
        // if it's the first iteration, the first set
        // of combinations is the same as the set of characters
        if (empty($combinations)) {
            $combinations = $chars;
        }
    
        // we're done if we're at size 1
        if ($size == 1) {
            return $combinations;
        }
    
        // initialise array to put new values in
        $new_combinations = array();
    
        // loop through existing combinations and character set to create strings
        foreach ($combinations as $combination) {
    
            foreach ($chars as $char) {
                $tmp = $combination. $char;
                if ($size == 2) {
                    $tmp .= '.com';
                }
    
                $new_combinations[] = $tmp;
                // I don't get what you were going to do with this line,
                // but this looks like a logical bug in your code
                //$new_combinations[] = implode(".com", $new_combinations);
            }
        }
    
        // call same function again for the next iteration
        return sampling($chars, $size - 1, $new_combinations);
    
    }
    
    
    // example
    $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
    
    $output = sampling($chars, 3);
    
    foreach($output as $do)
    {
        echo $do."<br>".PHP_EOL;
    }