Last active
February 26, 2018 06:06
-
-
Save nicopenaredondo/b37373e19747333a05009392f16adb97 to your computer and use it in GitHub Desktop.
CSV to Array
This file contains 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
private function csvToArray($filename='', $delimiter=',') | |
{ | |
$rows = array_map('str_getcsv', file($filename)); | |
$header = array_shift($rows); | |
$csv = array(); | |
foreach ($rows as $row) { | |
if(count($row) != count($header)) { | |
dd($row); | |
} | |
$csv[] = array_combine($header, $row); | |
} | |
return $csv; | |
} | |
//sample | |
public function handle() | |
{ | |
$this->info('Checking files..'); | |
$files = array_diff(scandir(storage_path('app/tmp/billers/bayd'), 1), array('..', '.')); | |
if(count($files) <= 0) { | |
$this->error('No files found.'); | |
return; | |
} | |
foreach($files as $file) { | |
$this->info("'$file' found. Now extracting data.. "); | |
$data = self::csvToArray(storage_path("app/tmp/billers/bayd/$file")); | |
self::saveToDatabase($data); | |
} | |
$bar = $this->output->createProgressBar(count($files)); | |
$bar->finish(); | |
$this->info("\nDone..."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment