问题描述
当我尝试将数组导出到csv文件时,我无法解决问题。我已经多次使用此函数,但在这里我看不到我的错误在哪里。
I can't solve a problem occuring when I try to export an array to a csv file. I have used this function several tumes 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 ... )
之后,我尝试将结果导出到csv by fputcsv:
After that I try to export the result to csv by fputcsv:
$fp = fopen('file.csv', 'w');
foreach ($mytags as $fields) {
fputcsv($fp, $fields);
}
但我收到此错误:
问题可能是只有一个字段吗?
另外,我尝试用 $ 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 theDoes 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 failure:给定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!