2

My $array looks like this in source code of my browser;

Array(
  [0] => 2015-01-15
  [1] => 2015-02-15
  [2] => 2015-03-15
)

My code to export this to a csv file is;

$fp = fopen("file.csv", "w");
fputcsv($fp, $array)
fclose($fp);

The output of the csv is horizontal. eg;

2015-01-01 | 2015-02-01 | 2015-03-01

I want it vertically;

2015-01-01
2015-02-01
2015-03-01

I don't know how to do this. I tried adding;

$fp = fopen("file.csv", "w", $delimiter = ',', $enclosure = '"');

And it didn't work. I tried creating a different array to test;

$array = array("2015-01-01", "2015-02-01", "2015-03-01");

And that gave the same horizontal result. I am open to changing the style/format of my array. Whatever gets the job done.

mikelovelyuk
  • 4,042
  • 9
  • 49
  • 95
  • if you just want a single value per line, why bother with csv at all? `fprint()` would serve you better without the overhead of generating a single-value csv record. – Marc B Apr 02 '15 at 14:20

1 Answers1

3

fputcsv() writes a line. For multiple lines you need to make multiple calls.

If each element in the array goes on a new line, just iterate the array:

   $fp = fopen("file.csv", "w");
    foreach($array as $element)
        fputcsv($fp, array($element));
    fclose($fp);

Note (as per comment by Rizier123) that you need to make $element an array.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Steve
  • 20,703
  • 5
  • 41
  • 67