1

Possible Duplicate:
How to create an array from a CSV file using PHP and the fgetcsv function

I have a file that contains multiple rows of comma delimited values. A sample of the file is:

1359739665,511251515,115151515,start,1277771750,,2215812581258,31.55.115.12,0,0 1359739665,511251515,115151515,restore,1277771750,,2215812581258,31.55.115.12,0,0 1359739665,511251515,115151515,restore,1277771750,,2215812581258,31.55.115.12,0,0 1359739665,511251515,115151515,end,1277771750,,2215812581258,31.55.115.12,0,0

I basically need to iterate through each row. At the moment, this is my code:

$file = "log.txt";
$contents = file_get_contents($file);
$fields = explode(',', $contents);

foreach ($fields as $row)
    {
       echo $row;
    }

This works fine for outputting the entire file. But my question is how do I iterate one row at a time. Each row essentially ends without the comma but I'm not sure how to force a break there. What I'd like to do is this (even though code is incorrect):

foreach ($fields as $row)  //need $row to equal each row of the log
  {
      echo $row[0];  // will output 1359739665
      echo $row[1];  // will output 511251515
      etc...
  }

Thank you!

Community
  • 1
  • 1
Jason
  • 1,105
  • 3
  • 16
  • 30
  • don't, use [fgetcsv](http://php.net/manual/en/function.fgetcsv.php) –  Feb 05 '13 at 02:51

5 Answers5

4

You should use either str_getcsv() or fgetcsv() to read CSV data.

An example using fgetcsv():

if (($handle = fopen("log.txt", "r")) !== FALSE) {
    while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
        echo $row[0] . "\n"; // Will output  data contained in the first column
    }
    fclose($handle);
}

An example using str_getcsv():

$file = "log.txt";
$contents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$csvRows = array_map('str_getcsv', $contents);

foreach($csvRows as $row) {
    echo $row[0] . "\n"; // Will output  data contained in the first column
}

Using explode() to manually parse CSV data might cause problems when your CSV fields may contain commas within their values or when the file endings of the CSV file isn't consistent. It is best to use the specialized functions described above as they are tailored for this specific task.

Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
  • thanks for the str_getcsv() tip! – hek2mgl Feb 05 '13 at 02:58
  • Thanks Andrew - that worked perfectly. Any preference to fgetcsv vs. str_getcsv? My files will always be identical in terms of the number of columns, etc. – Jason Feb 05 '13 at 02:59
  • [`fgetcsv()`](http://php.net/fgetcsv) is better for large data-sets as your file isn't loaded all at once into memory. For small to medium-size data-sets, [`str_getcsv()`](http://php.net/str_getcsv) is usually easier to implement and maintain. – Andrew Moore Feb 05 '13 at 03:00
1

Instead of using file_get_contents just use file:

$array_of_contents = file($file, FILE_IGNORE_NEW_LINES);

Once you've done that you can iterate over the array and get each individual "column" of the file with str_getcsv:

foreach (file($file, FILE_IGNORE_NEW_LINES) as $line) {
    foreach (str_getcsv($file) as $fields) {
        echo $fields[0];
    }
}
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

Count lines of .txt file, get one row at a time with foreach(), explode the row

William N
  • 432
  • 4
  • 12
0

Explode by new line

$fields = explode("\n", $contents);
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

Try this :

foreach(file('my.csv') as $line) {
    $record = str_getcsv($line, ',');
    var_dump($record);
}

The function file() returns an array with every single line of a file. It's very nice for iterating over the lines of a file. Then inside the foreach loop you can explode each line to single record.

Thanks to Andrew Moore I would advice you to use the function str_getcsv() instead of explode()

hek2mgl
  • 152,036
  • 28
  • 249
  • 266