@@ -116,39 +116,84 @@ public static function groupByField(array $array, string $field): array
116
116
return $ grouped ;
117
117
}
118
118
119
+
120
+ /**
121
+ * 读取 CSV 文件并返回数组格式
122
+ *
123
+ * @param string $filePath 文件路径
124
+ * @param bool $withHeader 第一行是否作为键名返回(默认 false)
125
+ *
126
+ * @return array 返回的二维数组
127
+ */
128
+ public static function csvToArray (string $ filePath , bool $ withHeader = false ): array
129
+ {
130
+ if (!file_exists ($ filePath )) {
131
+ throw new \Exception ("文件不存在: $ filePath " );
132
+ }
133
+
134
+ $ handle = fopen ($ filePath , 'r ' );
135
+ if (!$ handle ) {
136
+ throw new \Exception ("无法打开文件: $ filePath " );
137
+ }
138
+ $ result = [];
139
+ $ headers = [];
140
+ while (($ row = fgetcsv ($ handle )) !== false ) {
141
+ if ($ withHeader && empty ($ headers )) {
142
+ $ headers = $ row ;
143
+ continue ;
144
+ }
145
+ $ result [] = $ withHeader ? array_combine ($ headers , $ row ) : $ row ;
146
+ }
147
+ fclose ($ handle );
148
+ return $ result ;
149
+ }
150
+
119
151
/**
120
152
* 数组转换为 CSV 格式的字符串
121
153
*
122
- * @param array $array 数组
154
+ * @param array $array 数组 [['A1','A2','A3'],['B1','B2','B3']]
155
+ * @param string $filePath 输出文件路径,为空则返回字符串不输出到文件
123
156
* @param string $delimiter CSV 分隔符,默认为逗号
124
157
* @param string $enclosure CSV 包裹符号,默认为双引号
125
158
* @param string $escapeChar 转义符号,默认为反斜杠
126
159
*
127
- * @return string
160
+ * @return string|bool
128
161
*/
129
- public static function arrayToCsv (array $ array , string $ delimiter = ', ' , string $ enclosure = '" ' , string $ escapeChar = '\\' ): string
162
+ public static function arrayToCsv (array $ array , string $ filePath = '' , string $ delimiter = ', ' , string $ enclosure = '" ' , string $ escapeChar = '\\' ): string | bool
130
163
{
131
- // 开启输出缓冲区
132
- ob_start ();
133
- // 打开 PHP 输出流
134
- $ output = fopen ('php://output ' , 'w ' );
135
- // 检查文件句柄是否打开成功
136
- if ($ output === false ) {
137
- throw new \Exception ('无法打开输出流 ' );
164
+ // 如果传入了文件路径,则写入文件;否则使用输出缓冲区
165
+ if ($ filePath !== '' ) {
166
+ // 创建目录(如果不存在)
167
+ $ dir = dirname ($ filePath );
168
+ if (!is_dir ($ dir )) {
169
+ if (!mkdir ($ dir , 0777 , true ) && !is_dir ($ dir )) {
170
+ throw new \RuntimeException ("无法创建目录: $ dir " );
171
+ }
172
+ }
173
+ $ output = fopen ($ filePath , 'w ' );
174
+ if ($ output === false ) {
175
+ throw new \Exception ("无法写入文件: $ filePath " );
176
+ }
177
+ } else {
178
+ ob_start ();
179
+ $ output = fopen ('php://output ' , 'w ' );
180
+ if ($ output === false ) {
181
+ throw new \Exception ('无法打开输出流 ' );
182
+ }
138
183
}
139
- // 遍历数组并写入 CSV 格式
184
+ // 写入 CSV 数据
140
185
foreach ($ array as $ row ) {
141
186
if (!is_array ($ row )) {
142
187
throw new \Exception ('输入的每一行都必须是一个数组 ' );
143
188
}
144
189
fputcsv ($ output , $ row , $ delimiter , $ enclosure , $ escapeChar );
145
190
}
146
- // 关闭文件句柄
147
191
fclose ($ output );
148
- // 获取缓冲区内容
149
- return ob_get_clean ();
192
+ // 如果是写入文件,返回 true;否则返回 CSV 字符串
193
+ return $ filePath !== '' ? true : ob_get_clean ();
150
194
}
151
195
196
+
152
197
/**
153
198
* 解析 XML 数据并返回数组
154
199
*
0 commit comments