blob: ea82d4c1c7f544ded4b279a1da30c8e4da556132 [file] [log] [blame]
avm9996399bb77c2020-01-27 03:15:08 +01001<?php
2class csv {
avm9996399bb77c2020-01-27 03:15:08 +01003 public static function csv2array($file, $check = null, $onlyField = null) {
4 $return = [];
5
6 $flag = true;
7 $headers = [];
8 while (($line = fgetcsv($file, null, ",")) !== false) {
9 if ($flag) {
10 $headers = $line;
11 $flag = false;
12 } else {
13 $item = [];
14
15 foreach ($headers as $j => $field) {
16 $item[$field] = trim($line[$j]);
17 }
18
19 if ($check === null || $check($item)) $return[] = ($onlyField === null ? $item : ($item[$onlyField] ?? null));
20 }
21 }
22
23 fclose($file);
24
25 return $return;
26 }
27}