Created
January 6, 2019 06:51
-
-
Save jaesung2061/1bead909dc0cdd64c6a655afcf2d855a to your computer and use it in GitHub Desktop.
CSV to array function
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
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