0
votes

I have an array in the following format - it's essentially an array of preformatted CSV lines - key 0 and 1 in this example have a CSV value containing a line break.

Array
(
    [0] => 'foo,foo,foo,foo
    bar,foo,a:1:{i:0;s:4:"blah";}'
    [1] => 'bar,bar,bar,bar
    foo,bar,a:1:{i:0;s:4:"blah";}'
    [1] => 'bar,bar,bar,foo,bar,a:1:{i:0;s:4:"blah";}'
)

What I am doing next, is imploding that using \r\n as the glue, to generate the CSV file, then writing it out. The problem is that the resultant file does not wrap the fields containing a line break in double quotes so that the line break may be preserved (or at least I am assuming it will).

The CSV file generated will be uploaded to a database or edited in Excel and the line breaks need to be preserved in the field.

Using fputcsv after exploding into the right format generates a CSV which has the line break and wrapped in a double quote viewed in Notepad or similar but not when opened in Excel.

  $parent_wholelines = array();
  foreach ($output_array as $wholeline) {
    $parent_wholelines[] = explode(',', $wholeline);
  }
1
Prefer var_export. print_r isn't very good at showing us precisely what your data is.Lightness Races in Orbit
@Tomalak-Geret'kal - var_export outputs the same thing in this case, plus some per key wrapping quotes.Dan
Yes; those are useful. Precision is important in our field.Lightness Races in Orbit

1 Answers

0
votes