Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active July 4, 2018 17:04
Show Gist options
  • Save kjbrum/3181df2bc451bac361a4 to your computer and use it in GitHub Desktop.
Save kjbrum/3181df2bc451bac361a4 to your computer and use it in GitHub Desktop.
Create an associative array from a csv file.
<?php
/**
* Create an associative array from a csv file.
*
* @param file $file The csv file that is going to be parsed
* @return array $data An associative array with the csv headings as keys
*/
function csv_to_array( $file ) {
$data = array();
$header = null;
$file_handle = fopen( $file, 'r' );
while( $row = fgetcsv($file_handle ) ) {
if( $header === null ) {
$header = $row;
continue;
}
$data[] = array_combine( $header, $row );
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment