djekl
1/11/2013 - 8:29 PM

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

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);
}