Skip to content

Instantly share code, notes, and snippets.

@nicklasos
Created April 24, 2014 11:50
Show Gist options
  • Save nicklasos/11251754 to your computer and use it in GitHub Desktop.
Save nicklasos/11251754 to your computer and use it in GitHub Desktop.
CSV to array
<?php
function csv_to_array($filename, $delimiter = ',')
{
if (!file_exists($filename) || !is_readable($filename)) {
return false;
}
$header = null;
$data = [];
if (($handle = fopen($filename, 'r')) !== false) {
while (($row = fgetcsv($handle, 1000, $delimiter)) !== false) {
if(!$header) {
$header = $row;
} else {
$data[] = array_combine($header, $row);
}
}
fclose($handle);
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment