1

I have a form my company uses to create some files on the fly based on the input. I have a text area$fileString and they can enter file names followed by a hard return. I take that and create an array, $list. What i'm trying to do is if they enter in the text area two file names like this:

item1

item2

that it will create an array that contains 6 values, not two, so like this:

$genList = array(item1_f, item1_b, item1_i, item2_f, item2_b, item2_i);

I am getting this error though when I run my code:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 24 bytes) in /home/dcolor/public_html/dev/create.php on line 18

Line 18 is:

array_push($genList, $list[0] . "_f", $list[0] . "_b", $list[0] . "_i");

What am I doing wrong here? Code below

    $fileString = $_POST['fileList'];
    $unique = $_POST['unique'];
    $size = $_POST['size'];
    $generateArray = $_POST['generateArray'];
    $list = explode("\r\n",$fileString);

    if ($generateArray == "yes") {      
        if ($size == "5x7inimpos") {
            $genList = array();
            while (!empty($list)) {             
                array_push($genList, $list[0] . "_f", $list[0] . "_b", $list[0] . "_i");                    
            }
        }
        print_r($genlist);
    }
2
  • My first guess is that while(!empty($list)) {} is an infinite loop. Commented Sep 17, 2012 at 16:03
  • Number 2 in sidebar for related.. Commented Sep 17, 2012 at 16:04

3 Answers 3

3
while (!empty($list)) {             
        array_push($genList, $list[0] . "_f", $list[0] . "_b", $list[0] . "_i");                    
}

$list never gets emptied, you never get out of the while loop.

You just want to iterate over the list:

foreach($list as $element)
{

    array_push($genList, $element . "_f", $element . "_b", $element . "_i");                    

}
Sign up to request clarification or add additional context in comments.

Comments

2

The problem is

while (!empty($list)) {             
    array_push($genList, $list[0] . "_f", $list[0] . "_b", $list[0] . "_i");                    
}

Because $list will never be empty and thus causing an endless loop. PHP tries to push more and more elements into $genList until the memory limit exceeds.

You probably want to remove the element in the loop.

2 Comments

I don't understand why the $list will never be empty. If the original text area only had 4 file name values, $list would contain 4 array values. It would go empty after the 4th go through, correct?
No, because in the While loop you are not removing anything from list. You are reading the value in $list[0] an infinite amount of times :). In this case you could just iterate over the list like in moonwave99's answer.
1

This looks like an infinite loop to me. Why would $list ever become empty?

while (!empty($list)) {             
    array_push($genList, $list[0] . "_f", $list[0] . "_b", $list[0] . "_i");                    
}

Therefore, you will allocate memory with array_push until you hit your [256MB] memory limit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.