Skip to content

Instantly share code, notes, and snippets.

@ryaan-anthony
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save ryaan-anthony/e5b90516529f245a03bb to your computer and use it in GitHub Desktop.

Select an option

Save ryaan-anthony/e5b90516529f245a03bb to your computer and use it in GitHub Desktop.
<?php
class Import_Csv
{
public function load($filepath)
{
if(!file_exists($filepath)){
throw new Exception("File \"{$filepath}\" not found.");
exit;
}
$file = fopen($filepath, "r");
$mapped = $this->map($file);
fclose($file);
return $mapped;
}
protected function map($file)
{
$count = 0;
$columns = array();
$master = array();
while ($data = fgetcsv($file)) {
if(!$count++){
$columns = $data;
continue;
}
$results = array();
foreach($data as $key => $item){
$col = $columns[$key];
$results[$col] = $item;
}
$master[] = $results;
}
return $master;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment