Created
November 13, 2017 08:28
-
-
Save marianmirzacu/2133d3c71e14f6487ebcbd029f3efab2 to your computer and use it in GitHub Desktop.
PHP transform CSV to Array - DOES support line breaks in CSV fields
This file contains 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
public function csvToArray ($csv_file) { | |
$rows = $keys = $csv = array(); | |
if (($handle = fopen($csv_file->getRealPath(), "r")) !== FALSE) { | |
$i = 0; | |
while (($data = fgetcsv($handle, null, ";")) !== FALSE) { | |
$i++; | |
if ($i == 1) { | |
$keys = $data; | |
} | |
$rows[] = $data; | |
} | |
fclose($handle); | |
} | |
foreach ($rows as $i => $row) { | |
$csv[$i] = array_combine($keys, $row); | |
} | |
return $csv; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment