blob: d974713e3164ef30664bbb0102c4dbbcedabb584 [file] [log] [blame]
avm9996399bb77c2020-01-27 03:15:08 +01001<?php
2class 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}