avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 1 | <?php |
| 2 | class csv { |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 3 | 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 | } |