Skip to content

Instantly share code, notes, and snippets.

@kamaroly
Last active December 28, 2016 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamaroly/aaeb1e32dc287f054e52c311c648be9a to your computer and use it in GitHub Desktop.
Save kamaroly/aaeb1e32dc287f054e52c311c648be9a to your computer and use it in GitHub Desktop.
Php method / function to convert CSV into an ARRAY
<?php
/**
* Convert CSV TO ARRAY
* @param string $filename
* @param string $delimiter
* @return
*/
function csvToArray($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){
// Remove any space in the header
$header = array_map('trim', $row);
}
$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