Friday, 15 May 2015

php - Allowed memory size of 268435456 bytes exhausted with array -



php - Allowed memory size of 268435456 bytes exhausted with array -

i trying create function generate possible combinations. part works output

aaa aab aac aad ...

but trying add together extension each of combinations want add together "hi" @ end like

aaahi aabhi aachi aadhi

iv tried next im getting error. there improve way doing?

fatal error: allowed memory size of 268435456 bytes exhausted (tried allocate 100663409 bytes)

here script

function sampling($chars, $size, $combinations = array()) { # if it's first iteration, first set # of combinations same set of characters if (empty($combinations)) { $combinations = $chars; } # we're done if we're @ size 1 if ($size == 1) { homecoming $combinations; } # initialise array set new values in $new_combinations = array(); # loop through existing combinations , character set create strings foreach ($combinations $combination) { foreach ($chars $char) { $new_combinations[] = $combination. $char; $new_combinations[] = implode($new_combinations, "hi"); } } # phone call same function 1 time again next iteration homecoming sampling($chars, $size - 1, $new_combinations); } // illustration $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 $do) { echo $do."<br>"; }

this not clear going do, first of all, using implode() incorrectly. first argument must $glue , sec array.

string implode ( string $glue , array $pieces )

secondly, $new_combinations array growing progressively on each step.

but if understood going do, code work you:

<?php function sampling($chars, $size, $combinations = array()) { // if it's first iteration, first set // of combinations same set of characters if (empty($combinations)) { $combinations = $chars; } // we're done if we're @ size 1 if ($size == 1) { homecoming $combinations; } // initialise array set new values in $new_combinations = array(); // loop through existing combinations , character set create strings foreach ($combinations $combination) { foreach ($chars $char) { $tmp = $combination. $char; if ($size == 2) { $tmp .= '.com'; } $new_combinations[] = $tmp; // don't going line, // looks logical bug in code //$new_combinations[] = implode(".com", $new_combinations); } } // phone call same function 1 time again next iteration homecoming sampling($chars, $size - 1, $new_combinations); } // illustration $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 $do) { echo $do."<br>".php_eol; }

php arrays combinations memory-limit

No comments:

Post a Comment