将数组导出到csv failure:给定字符串 [英] Export an array to csv failure : string given

查看:100
本文介绍了将数组导出到csv failure:给定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

mysql_fetch_array()期望参数1是资源,在选择中给定布尔值

当我尝试将数组导出到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);
}

但我收到此错误:


警告: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 failure:给定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆