Created
October 6, 2018 05:33
-
-
Save narwanimonish/1b229cc1ddd7cfd25bab138927f6d4e1 to your computer and use it in GitHub Desktop.
CSV Helper to parse CSV file contents into php Array with headers
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 | |
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