Created
January 11, 2013 20:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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