Created
October 13, 2014 03:17
-
-
Save augustyip/e66cfa048d5186879ada to your computer and use it in GitHub Desktop.
Convert the csv file to 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 the csv file to array. | |
* @param string $filename | |
* @param string $delimiter | |
* @return Array | |
*/ | |
function isc_data_csv_to_array($filename='', $delimiter=',') { | |
if(!file_exists($filename) || !is_readable($filename)) { | |
return FALSE; | |
} | |
ini_set("auto_detect_line_endings", true); | |
$header = NULL; | |
$data = array(); | |
if (($handle = fopen($filename, 'r')) !== FALSE) { | |
while (($row = fgetcsv($handle, 4000, $delimiter)) !== FALSE) { | |
if(!$header) { | |
$header = $row; | |
} else { | |
$header_count = count($header); | |
$row_count = count($row); | |
if ($header_count > $row_count) { | |
$array_fill = array_fill($row_count, $header_count - $row_count, 0); | |
$row += $array_fill; | |
} | |
$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