Php csv中的多列

Php csv中的多列,php,csv,multiple-columns,Php,Csv,Multiple Columns,我正在用php创建一个可下载的csv。这就是我到目前为止所做的: // output headers so that the file is downloaded rather than displayed header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=data.csv'); // create a file pointer conne

我正在用php创建一个可下载的csv。这就是我到目前为止所做的:

 // output headers so that the file is downloaded rather than displayed
 header('Content-Type: text/csv; charset=utf-8');
 header('Content-Disposition: attachment; filename=data.csv');

 // create a file pointer connected to the output stream
 $output = fopen('php://output', 'w');

 // output the column headings
 fputcsv($output, array('id', 'role_id', 'parent_id'));

 $list = RoleQuery::create()->find();
 $list = $list->toArray();

 $list = array_values($list);
 // loop over the rows, outputting them
 foreach($list as $fields){
     fputcsv($output, $fields);
 }
为了进行测试,我将输出数据库中的角色。问题是它们都在一列中,如下所示:


如何确保id、角色id和父id位于不同的列中?

您的CSV数据看起来很好,但您用来打开CSV的任何工具似乎都没有使用逗号作为分隔符。正如davidkonrad所建议的,您可以将每个值用引号括起来,看看这是否有帮助。但通常,在Google Docs或Excel中打开CSV时,它会询问您希望如何划分,并且通常默认为逗号。

在客户端上使用真正知道如何打开CSV文件的软件?服务器上(在本例中)所需的只是一个逗号,而您似乎拥有它。我用Excel打开了它,它给出了这个结果。。Excel知道如何打开csv文件?这完全取决于读者。在大多数阅读器(如Excel或LibreOffice)中,加载文件时有几个选项。大多数读者很乐意将Yout输出作为一个多coulmn CSV。但是,我会在每个单元格/列周围加上引号“”。