Skip to content

Instantly share code, notes, and snippets.

@benigno027
Forked from benbalter/parse-csv.php
Created August 30, 2019 21:46
Show Gist options
  • Save benigno027/97a29ba02de01e4b8e557790c76957ef to your computer and use it in GitHub Desktop.
Save benigno027/97a29ba02de01e4b8e557790c76957ef to your computer and use it in GitHub Desktop.
Parse CSV into Associative Array
<?php
$lines = explode( "\n", file_get_contents( 'input.csv' ) );
$headers = str_getcsv( array_shift( $lines ) );
$data = array();
foreach ( $lines as $line ) {
$row = array();
foreach ( str_getcsv( $line ) as $key => $field )
$row[ $headers[ $key ] ] = $field;
$row = array_filter( $row );
$data[] = $row;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment