0

i'm trying to create a csv file with emailadresses. But when i output the php array with fputcsv the output is Array,Array in stead of the data inside the array

I hope someone can help..

i have tried to remove all the lines that start with header( but then the same output comes to the screen.

Code:

header('Content-Description: File Transfer');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=emails.csv');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$out = fopen('php://output', 'w');
fputcsv($out, array(
    array('email', 'aantal keer ingevuld'),
    array('[email protected]', '8')
    )
);
fclose($out);
exit;

Output of a var_dump of the array:

array(2) { [0]=> array(2) { [0]=> string(5) "email" [1]=> string(20) "aantal keer ingevuld" } [1]=> array(2) { [0]=> string(24) "[email protected]" [1]=> string(1) "8" } }

output of the csv file:

Array,Array

Problem solved fputcsv only puts 1 array at a time. i have to use a loop with a multidimensional array.

Thanks for all the help!

1
  • 2
    fputcsv() writes 1 line at a time, from a simple array; not an entire file in a single call, from a multidimensional arry Commented Mar 16, 2017 at 9:22

1 Answer 1

3

You have to put the fields of the array in separately. Like this:

$list = array(
    array('email', 'aantal keer ingevuld'),
    array('[email protected]', '8')
)   

foreach ($list as $fields) {
    fputcsv($out, $fields);
}

Check the documentation on fputcsv() for more information.

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

1 Comment

In 8 minutes i can accept your anwser, thanks this worked.

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.