Skip to content

Instantly share code, notes, and snippets.

@jaesung2061
Created January 6, 2019 06:51
Show Gist options
  • Save jaesung2061/1bead909dc0cdd64c6a655afcf2d855a to your computer and use it in GitHub Desktop.
Save jaesung2061/1bead909dc0cdd64c6a655afcf2d855a to your computer and use it in GitHub Desktop.
CSV to array function
function csv_to_array($fileName, $delimiter) {
$parsed = [];
$line = 1;
$handle = fopen($fileName, 'r');
while (($data = fgetcsv($handle, 0, $delimiter)) !== false) {
$line++;
$row = [];
for ($i = 0; $i < count($data); $i++) {
$row[] = $data[$i];
}
$parsed[] = $row;
}
fclose($handle);
$headers = array_splice($parsed, 0, 1)[0];
return array_map(function ($row) use ($headers) {
return array_combine($headers, $row);
}, $parsed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment