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?
+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
2009-09-03 11:21:21
+1 for including example (:
peirix
2009-09-03 11:26:16
A:
You can use file().
<?php
$file_arr = file(/path/file);
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num}: " . $line;
}
?>
danielv
2009-09-03 11:25:10