Skip to content

Instantly share code, notes, and snippets.

@Joemires
Forked from benbalter/parse-csv.php
Created November 22, 2021 12:40
Show Gist options
  • Save Joemires/44c543e4ad2cd517e928a22434af46e6 to your computer and use it in GitHub Desktop.
Save Joemires/44c543e4ad2cd517e928a22434af46e6 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