Skip to content

Instantly share code, notes, and snippets.

@anunay
Last active January 5, 2022 19:38
Show Gist options
  • Save anunay/6358540 to your computer and use it in GitHub Desktop.
Save anunay/6358540 to your computer and use it in GitHub Desktop.
Convert a CSV file to a PHP associated array
<?php
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
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