Working with CSV files in PHP

This is the best and simplest way to work with a CSV file that has the headers on the first row in PHP.


if (($handle = fopen('csv.csv', 'r')) !== false)
{
$length = 0;
$delimiter = ',';
$enclosure = '"';
$escape = '\\';

if(($header_line = fgetcsv($handle, $length, $delimiter, $enclosure, $escape)) !== false)
{
while(($line = fgetcsv($handle, $length, $delimiter, $enclosure, $escape)) !== false)
{
$data = array_combine($header_line, $line);
//do stuff here
}
}

fclose($handle);
}


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>