1

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.

I set an array:

$mytags= array();

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  ... )

After that I try to export the result to CSV by fputcsv:

$fp = fopen('file.csv', 'w');
foreach ($mytags as $fields) {
    fputcsv($fp, $fields);
}

But I get this error:

Warning: fputcsv() expects parameter 2 to be array, string given in C:\wamp\www\tests\capturetags.php on line 55

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?

0

1 Answer 1

6

The error is very clear, $fields is not an array, it is a string. You need an array.

fputcsv($fp, $mytags);

Without a foreach loop will do the job.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot but i am shameful to fail on a so simple error. But i have to say that this function is not weel documented for a beginner :)
@user1453786 You'll need to study code documentation more closely if you expect to find many answers for yourself in the future. PHP's documentation is almost as straight-forward as it can get int fputcsv ( resource $handle , array $fields [, string $delimiter = ',' [, string $enclosure = '"' ]] )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.