Skip to content

Instantly share code, notes, and snippets.

@ctrl-freak
Created September 9, 2011 02:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctrl-freak/1205392 to your computer and use it in GitHub Desktop.
Save ctrl-freak/1205392 to your computer and use it in GitHub Desktop.
Read CSV File with Headers to Array
<?
$filename = $_POST['filename'];
$entries = array();
$row = 1;
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($row == 1) {
$columns = $data;
for ($i = 0; $i < count($columns); $i++) {
$col = explode(' ', $columns[$i]);
$columns[$i] = strtolower($col[0]);
}
} else {
$entry = array();
for ($c=0; $c < count($data); $c++) {
$entry[$columns[$c]] = $data[$c];
}
$entries[] = $entry['email'];
}
$row++;
}
fclose($handle);
}
// print_r($entries);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment