Last active
December 28, 2016 09:21
-
-
Save kamaroly/aaeb1e32dc287f054e52c311c648be9a to your computer and use it in GitHub Desktop.
Php method / function to convert CSV into an ARRAY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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