【问题标题】:Allowed memory size of 268435456 bytes exhausted with array允许的内存大小为 268435456 字节,数组已用尽
【发布时间】:2014-06-22 15:11:24
【问题描述】:

我正在尝试创建一个函数来生成所有可能的组合。这部分效果很好,所以输出将是

aaa aab aac 广告 ...

但现在我正在尝试为每个组合添加扩展名,所以我想在末尾添加“HI”

aaaHI aabHI aacHI aadHI

我尝试了以下但我得到了这个错误。有没有比我正在做的更好的方法来做到这一点?

致命错误:允许的内存大小为 268435456 字节已用尽(尝试分配 100663409 字节)

这是我的脚本

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>";
} 

【问题讨论】:

  • 要么使用 set_ini 函数,要么打开 php.ini 并增加允许的内存大小。想一想为什么在增加大小之前确实需要这样做。
  • iv 试过了,但还是不行
  • 您要创建多大的数组?
  • 如果我不使用 $new_combinations[] = implode($new_combinations, "HI");它工作正常。但是如果我添加这一行,它就不起作用了
  • 您的 $new_comninations 数组逐渐增长。因为您在上一次迭代中获取所有组合并在下一次迭代中添加它们。当然,您的内存不足。只是不要那样做。

标签: php arrays combinations memory-limit


【解决方案1】:

这不是很清楚你要做什么,但首先,你错误地使用了implode()。第一个参数必须是 $glue,第二个是你的数组。

string implode ( string $glue , array $pieces )

其次,您的 $new_combinations 数组每一步都在逐步增长。

但如果我明白你要做什么,这段代码对你有用吗:

<?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;
}

【讨论】:

  • 谢谢我试过了,但它的显示像“aa.coma.com”而不是“aa.com”
猜你喜欢
  • 2011-04-23
  • 2012-12-27
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 2014-03-07
相关资源
最近更新 更多