Skip to content

Instantly share code, notes, and snippets.

@marianmirzacu
Created November 13, 2017 08:28
Show Gist options
  • Save marianmirzacu/2133d3c71e14f6487ebcbd029f3efab2 to your computer and use it in GitHub Desktop.
Save marianmirzacu/2133d3c71e14f6487ebcbd029f3efab2 to your computer and use it in GitHub Desktop.
PHP transform CSV to Array - DOES support line breaks in CSV fields
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