|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Hejunjie\Csv; |
| 4 | + |
| 5 | +use Exception; |
| 6 | + |
| 7 | +/** |
| 8 | + * CSV处理类 |
| 9 | + * |
| 10 | + * @package Hejunjie\Csv |
| 11 | + */ |
| 12 | +class Csv |
| 13 | +{ |
| 14 | + |
| 15 | + /** |
| 16 | + * 读取 CSV 文件并转换为数组 |
| 17 | + * |
| 18 | + * @param string $csvPath CSV 文件路径 |
| 19 | + * @param bool $useHeaderAsKey 是否使用第一行作为 key |
| 20 | + * @param string $delimiter CSV 分隔符,默认逗号 |
| 21 | + * |
| 22 | + * @return array |
| 23 | + * @throws Exception |
| 24 | + */ |
| 25 | + function readCsvToArray(string $csvPath, bool $useHeaderAsKey = true, string $delimiter = ','): array |
| 26 | + { |
| 27 | + if (!is_readable($csvPath)) { |
| 28 | + throw new \Exception("CSV 文件不可读: {$csvPath}"); |
| 29 | + } |
| 30 | + $handle = fopen($csvPath, 'r'); |
| 31 | + if ($handle === false) { |
| 32 | + throw new \Exception("无法打开 CSV 文件: {$csvPath}"); |
| 33 | + } |
| 34 | + $result = []; |
| 35 | + $header = []; |
| 36 | + while (($row = fgetcsv($handle, 0, $delimiter)) !== false) { |
| 37 | + if ($useHeaderAsKey && empty($header)) { |
| 38 | + $header = $row; |
| 39 | + continue; |
| 40 | + } |
| 41 | + if ($useHeaderAsKey) { |
| 42 | + $item = []; |
| 43 | + foreach ($header as $index => $key) { |
| 44 | + $item[$key] = $row[$index] ?? null; |
| 45 | + } |
| 46 | + $result[] = $item; |
| 47 | + } else { |
| 48 | + $result[] = $row; |
| 49 | + } |
| 50 | + } |
| 51 | + fclose($handle); |
| 52 | + return $result; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * 将数组写入 CSV 文件 |
| 57 | + * |
| 58 | + * @param array $data 要写入的数据 |
| 59 | + * @param string $filePath CSV 文件保存路径 |
| 60 | + * @param bool $writeHeader 是否写入表头(仅在二维关联数组时有效) |
| 61 | + * @param string $delimiter CSV 分隔符,默认逗号 |
| 62 | + * |
| 63 | + * @return void |
| 64 | + * @throws Exception |
| 65 | + */ |
| 66 | + function arrayToCsv(array $data, string $filePath, bool $writeHeader = true, string $delimiter = ','): void |
| 67 | + { |
| 68 | + if (empty($data)) { |
| 69 | + throw new \Exception('写入 CSV 的数据为空'); |
| 70 | + } |
| 71 | + // 确保目录存在 |
| 72 | + $dir = dirname($filePath); |
| 73 | + if (!is_dir($dir)) { |
| 74 | + if (!mkdir($dir, 0777, true) && !is_dir($dir)) { |
| 75 | + throw new \Exception("无法创建目录: {$dir}"); |
| 76 | + } |
| 77 | + } |
| 78 | + $handle = fopen($filePath, 'w'); |
| 79 | + if ($handle === false) { |
| 80 | + throw new \Exception("无法写入 CSV 文件: {$filePath}"); |
| 81 | + } |
| 82 | + fwrite($handle, "\xEF\xBB\xBF"); |
| 83 | + $firstRow = reset($data); |
| 84 | + $isAssoc = array_keys($firstRow) !== range(0, count($firstRow) - 1); |
| 85 | + if ($writeHeader && $isAssoc) { |
| 86 | + fputcsv($handle, array_keys($firstRow), $delimiter); |
| 87 | + } |
| 88 | + // 写数据行 |
| 89 | + foreach ($data as $row) { |
| 90 | + if ($isAssoc) { |
| 91 | + fputcsv($handle, array_values($row), $delimiter); |
| 92 | + } else { |
| 93 | + fputcsv($handle, $row, $delimiter); |
| 94 | + } |
| 95 | + } |
| 96 | + fclose($handle); |
| 97 | + } |
| 98 | +} |
0 commit comments