2

I need to create a CSV file from a PHP array, I'm aware of the fputcsv() function. I've a recollection that a better function exists for this kind of stuff but I can't find it nor remember it's name.

Does anyone know of such function or am I imagining?


I might be making some confusion with the fgetcsv() function and the str_getcsv() equivalent.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500

1 Answers1

2

fputcsv is to write an array into a file with CSV format. fgetcsv is to read data from a file with CSV format and convert it into an array. And str_getcsv is to convert a string in CSV format into an array.

So fputcsv and fgetcsv are their respective inverses. And fgetcsv could be a function that uses str_getcsv.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • I know that, but `fgetcsv()` is to `str_getcsv()` as `fputcsv()` is to `...`? – Alix Axel Jan 25 '10 at 07:54
  • 1
    @Alix Axel: Ah, now I get what you’re asking form. Well, PHP has no such function. But there are some suggestions in the comments to http://php.net/str_getcsv that use a temporary stream buffer that can be written with `fputcsv` and then be read. – Gumbo Jan 25 '10 at 07:58
  • Something that converts a an array into a CSV string but PHP isn't really well known for it's stdlib API design and I can't seem to find an actual function that does this. – Noufal Ibrahim Jan 25 '10 at 07:58
  • Sorry, sometimes I've some trouble expressing myself well in English. Thanks for the confirmation though, my brain tricked me on this one. =) – Alix Axel Jan 25 '10 at 08:02