我在csv出口部的工作不多,现在我有问题了。我有这样的事情:
['client']=>
['name']=>'John',
['surname']=>'Doe',
['things']=>
[0]=>'jacket',
[1]=>'shoes',
[2]=>'hat'当我为csv解析ti时,我得到
name | surname | things
John | Doe | jacket, shoes, hat我想让
name | surname | things
John | Doe | jacket
shoes
hat有什么方法可以在PHP中实现吗?
编辑:我试过这样做:
$client['things'] = ",".implode(",", $client['things']);我也试过这个
$client['things'] = ",".implode("\n", $client['things']);还有这个
$client['things'] = ",".implode("\r", $client['things']);而且它没有正常工作。
发布于 2014-05-27 14:35:48
CSV不支持嵌套的值集。
也符合用于CSV格式的RFC
每个记录都位于一个单独的行上,由一个断行(CRLF)分隔。
和
包含换行符(CRLF)、双引号和逗号的字段应该用双引号括起来.
您必须导出未转义的新行分隔符、逗号和引号,我建议您使用fputcsv()函数为大多数程序和库生成有效和正确的CSV。
例如:
<?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);输出:John,Doe,"jacket,shoes,hat"与CRLF结束。
https://stackoverflow.com/questions/23890551
复制相似问题