Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Created March 3, 2014 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tegansnyder/9315937 to your computer and use it in GitHub Desktop.
Save tegansnyder/9315937 to your computer and use it in GitHub Desktop.
Read a CSV file to an associate array
<?php
$csv = array();
if (($handle = fopen('your_csv_file_name_here.csv', "r")) !== FALSE) {
$rowCounter = 0;
while (($rowData = fgetcsv($handle, 0, ",")) !== FALSE) {
if (0 === $rowCounter) {
$headerRecord = $rowData;
} else {
foreach ($rowData as $key => $value) {
$csv[$rowCounter - 1][$headerRecord[$key]] = $value;
}
}
$rowCounter++;
}
fclose($handle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment