Save array to different columns with fputcsv

Asked

Viewed 308 times

0

I am trying to output some data from BD to csv with the following code:

while($linha=$buscar->fetch(PDO::FETCH_ASSOC)){
   fputcsv($out, $linha);
}
   fclose( $out );
}

which returns all the contents of the array in a single column. How could I get each position of the array to be in a column in csv?

OBS:

print_r($linha);

results in :

   Array
   (
     [data] => 20/05/2017
     [descricao] => pag 
     [pagamento] => 500
   )

Where they all stay in the same column in csv, and in case the ideal would be for each one to stay in a column.

  • Could put the result of print_r($linha); in the question?

  • Sorry, I did not understand very well, I must put in the code posted here?

  • That, edit the question and put the result. Your code seems correct I just don’t understand how everything looks in one column.

  • the code is really correct, it does what I intend, but I would like a code "more" to organize it by columns, because the one I presented creates a row per loop in csv, but does not create a column per array, which in this case is what I want.

  • 1

    Add Problem Solution as Answer, not Question Editing.

1 answer

2


Problem solved by adding a ; at the end of the fput.

fputcsv($out, $linha, ';');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.