Skip to content

Instantly share code, notes, and snippets.

@narwanimonish
Created October 6, 2018 05:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save narwanimonish/1b229cc1ddd7cfd25bab138927f6d4e1 to your computer and use it in GitHub Desktop.
Save narwanimonish/1b229cc1ddd7cfd25bab138927f6d4e1 to your computer and use it in GitHub Desktop.
CSV Helper to parse CSV file contents into php Array with headers
<?php
class CSVHelper
{
// Build wonderful things
public static function parseCSV($fileName)
{
$extractData = [];
$csvArray = [];
if($fileName) {
$fp = fopen($fileName, 'r');
if($fp) {
while ($row = fgetcsv($fp)) {
$csvArray[] = $row;
}
fclose($fp);
$headers = [];
foreach($csvArray as $key=>$data) {
if($key == 0) {
$headers = $data;
continue;
}
$temp = [];
foreach($data as $key1=>$value) {
$temp[$headers[$key1]] = $value;
}
$extractData[] = $temp;
}
}
}
return $extractData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment