tags:

views:

153

answers:

6

I have text file with some stuff that i would like to put into array. That text file has one value per line. How do i put each line into array?

+1  A: 

use file()

http://se2.php.net/manual/en/function.file.php

William Macdonald
+4  A: 

use the file() function - easy!

$lines=file('file.txt');

If you want to do some processing on each line, it's not much more effort to read it line by line with fgets()...

$lines=array();
$fp=fopen('file.txt', 'r');
while (!feof($fp))
{
    $line=fgets($fp);

    //process line however you like
    $line=trim($line);

    //add to array
    $lines[]=$line;

}
fclose($fp);
Paul Dixon
+1 for including example (:
peirix
A: 
$fileArr = file("yourfile.txt")

http://www.php.net/manual/en/function.file.php

Aaron W.
A: 

file will return an array of the file content where each element corresponds to one line of the file (with line ending character seqence).

Gumbo
A: 
$lines = file('file.txt');

Documentation

Øystein Riiser Gundersen
A: 

You can use file().

<?php
$file_arr = file(/path/file);
foreach ($lines as $line_num => $line) {
    echo "Line #{$line_num}: " . $line;
}
?>

php.net file()

danielv