Last active
August 29, 2015 14:08
-
-
Save ryaan-anthony/e5b90516529f245a03bb to your computer and use it in GitHub Desktop.
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 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