Skip to content

Instantly share code, notes, and snippets.

@rotespferd
Created September 20, 2012 12:01
Show Gist options
  • Save rotespferd/3755496 to your computer and use it in GitHub Desktop.
Save rotespferd/3755496 to your computer and use it in GitHub Desktop.
Get data out of a CSV-file.
function getCSVData($file)
{
$content = array();
$columNames = array();
$row = 0;
$handle = fopen($file, "r");
while (($data = fgetcsv($handle, 100000, CSVLIMITER, '"')) !== false) {
$row++;
if ($row == 1) {
$columNames = $data;
} else {
$tmp = $data;
$data = array();
for ($j = 0; $j < count($columNames); $j++) {
$value = trim(utf8_encode(array_shift($tmp)));
$key = trim(utf8_encode($columNames[$j]));
$data[$key] = ($value == "NULL") ? null : trim($value);
}
$content[] = $data;
}
}
fclose($handle);
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment