将数组导出为CSV失败:给定字符串 [英] Export an array to CSV failure: string given
问题描述
当我尝试将数组导出到CSV文件时,我无法解决出现的问题.我已经多次使用此功能了,但是没有出现错误.
I can't solve a problem occurring when I try to export an array to a CSV file. I have used this function several times without problem but here I do not see where my mistake is.
我设置了一个数组:
$mytags= array();
我通过循环填充它.当我通过 print_r($ mytags);
打印内容时似乎还可以,下面是我的输出示例:
I populate it by a loop. When I print the content through print_r($mytags);
it seems to be OK, here some examples of my output:
Array ( [0] => [1] => air-travel [2] => airports [3] => security-airport [4] => city-airport ... )
之后,我尝试通过fputcsv将结果导出到CSV:
After that I try to export the result to CSV by fputcsv:
$fp = fopen('file.csv', 'w');
foreach ($mytags as $fields) {
fputcsv($fp, $fields);
}
但是我得到这个错误:
警告:fputcsv()期望参数2为数组,字符串为C:\ wamp \ www \ tests \ capturetags.php,第55行
Warning: fputcsv() expects parameter 2 to be array, string given in C:\wamp\www\tests\capturetags.php on line 55
问题可能是只有一个字段吗?另外,我尝试用 $ mytags
替换 $ fields
来编写CSV,在这种情况下,我得到了4GB的文件,所以它不是有人看到如何在CSV文件中记录其唯一字段吗?
Could the problem be that there is only one field?
Alternatively, I tried to replace $fields
by $mytags
to write the CSV, and in this case I get a 4GB files, so it's not the
Does someone see how to record thhis unique field in a CSV file?
推荐答案
错误非常明显, $ fields
不是数组,而是字符串.您需要一个数组.
The error is very clear, $fields
is not an array, it is a string. You need an array.
fputcsv($fp, $mytags);
没有foreach循环将完成这项工作.
Without a foreach loop will do the job.
这篇关于将数组导出为CSV失败:给定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!