Created
September 9, 2011 02:58
-
-
Save ctrl-freak/1205392 to your computer and use it in GitHub Desktop.
Read CSV File with Headers to Array
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
<? | |
$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