Skip to content

Instantly share code, notes, and snippets.

@mattkirwan
Created January 11, 2013 20:29
Show Gist options
  • Save mattkirwan/4513722 to your computer and use it in GitHub Desktop.
Save mattkirwan/4513722 to your computer and use it in GitHub Desktop.
This gist uses PHP's built in SplFileObject class to loop through .csv file and pretty much do what you want with each row of data. Probably one of my most used snippets of code.
<?php
$file = 'data.csv';
try {
$csv = new SplFileObject($file, 'r');
} catch (RuntimeException $e) {
printf("Error opening .csv: %s\n", $e->getMessage());
}
while(!$csv->eof() && ($row = $csv->fgetcsv()) && $row[0] !== null)
{
// Each row of data is available as an array
print_r($row);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment