Skip to content

Instantly share code, notes, and snippets.

@augustyip
Created October 13, 2014 03:17
Show Gist options
  • Save augustyip/e66cfa048d5186879ada to your computer and use it in GitHub Desktop.
Save augustyip/e66cfa048d5186879ada to your computer and use it in GitHub Desktop.
Convert the csv file to array.
<?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