I haven't worked that much with csv export, and now I have a problem. I have something like this:
['client']=>
['name']=>'John',
['surname']=>'Doe',
['things']=>
[0]=>'jacket',
[1]=>'shoes',
[2]=>'hat'
And when I parse ti for csv I get
name | surname | things
John | Doe | jacket, shoes, hat
And I would like to get
name | surname | things
John | Doe | jacket
shoes
hat
Is there a way to do it in PHP?
EDIT: I tried this:
$client['things'] = ",".implode(",", $client['things']);
I tried this also
$client['things'] = ",".implode("
", $client['things']);
and this
$client['things'] = ",".implode("", $client['things']);
and it didn't worked properly.
CSV doesn't support nested sets of values.
Also according with RFC for CSV format:
Each record is located on a separate line, delimited by a line break (CRLF).
and
Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.
You must exlude unescaped new line separators, commas and quote symbols and I suggest you to use fputcsv() function to generate valid and correct CSV for most programs and libraries.
E.g.:
<?php
$row = array(
'name' => 'John',
'surname' => 'Doe',
'things' => array('jacket', 'shoes', 'hat'),
);
$row = array_map(function ($value) {
return is_array($value) ? implode(',', $value) : $value;
}, $row);
$output = fopen('php://output', 'w');
fputcsv($output, $row);
Outputs: John,Doe,"jacket,shoes,hat"
with CRLF ending.