Skip to content

Instantly share code, notes, and snippets.

@wernersmit
Created March 11, 2017 13:09
Show Gist options
  • Save wernersmit/8ad97f554cded0de5ec79db15a34dfb4 to your computer and use it in GitHub Desktop.
Save wernersmit/8ad97f554cded0de5ec79db15a34dfb4 to your computer and use it in GitHub Desktop.
Read CSV file and convert to array using fgetcsv
<?php
/*
* Source: http://www.codedevelopr.com/articles/reading-csv-files-into-php-array/
*/
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
// Set path to CSV file
$csvFile = 'test.csv';
$csv = readCSV($csvFile);
echo '<pre>';
print_r($csv);
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment