Last active
July 4, 2018 17:04
-
-
Save kjbrum/3181df2bc451bac361a4 to your computer and use it in GitHub Desktop.
Create an associative array from a csv file.
This file contains hidden or 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
<?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