avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 1 | <?php |
| 2 | class csv { |
| 3 | public static $fields = ["dni", "name", "category", "email", "companies"]; |
| 4 | |
| 5 | public static function csv2array($file, $check = null, $onlyField = null) { |
| 6 | $return = []; |
| 7 | |
| 8 | $flag = true; |
| 9 | $headers = []; |
| 10 | while (($line = fgetcsv($file, null, ",")) !== false) { |
| 11 | if ($flag) { |
| 12 | $headers = $line; |
| 13 | $flag = false; |
| 14 | } else { |
| 15 | $item = []; |
| 16 | |
| 17 | foreach ($headers as $j => $field) { |
| 18 | $item[$field] = trim($line[$j]); |
| 19 | } |
| 20 | |
| 21 | if ($check === null || $check($item)) $return[] = ($onlyField === null ? $item : ($item[$onlyField] ?? null)); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fclose($file); |
| 26 | |
| 27 | return $return; |
| 28 | } |
| 29 | } |