Page 1 of 1

Create variable name while inside a while loop

Posted: Thu Sep 29, 2011 11:00 am
by davidhopkins
Hello all.

My code is quite basic, a while loop that echos each line of a file. However i need to explode each line of data into a new array for every line.

My code as it was it

Code: Select all

while (($buffer = fgets($FileHandle, 4096)) !== false) {
            echo $buffer."<br>";
        }
Which just echos each line to the screen.

What i need is as follows

Code: Select all

        while (($buffer = fgets($FileHandle, 4096)) !== false) {
            $line = explode('|',$buffer);
        }
My problem however is $line needs to be changed each time the while loop is run. What would be ideal is if i could add an incrementing number at the end of $line.

Any help would be great :(

Thanks

Re: Create variable name while inside a while loop

Posted: Thu Sep 29, 2011 12:11 pm
by AbraCadaver
I would use file, but it is basically:

Code: Select all

foreach(file('/path/to/file.txt') as $line) {
   $lines[] = explode('|', $line);
}
print_r($lines);