Skip to content

Instantly share code, notes, and snippets.

@nicopenaredondo
Last active February 26, 2018 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicopenaredondo/b37373e19747333a05009392f16adb97 to your computer and use it in GitHub Desktop.
Save nicopenaredondo/b37373e19747333a05009392f16adb97 to your computer and use it in GitHub Desktop.
CSV to Array
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