简体   繁体   English

列的php CSV标头

[英]php CSV headers for columns

I want to write the columns for a generated CSV file into PHP headers. 我想将生成的CSV文件的列写入PHP标头。 I want the headers to be (name, surname, address). 我希望标题为(名称,姓氏,地址)。

Image Illustration: 图片说明:

在此处输入图片说明

This is my code so far: 到目前为止,这是我的代码:

if (! empty ( $_POST ['csv'] )) 
{
   $this->prepareAckINS ( $rows, true ); 
   header ('Content-Type: text/csv; utf-8'); 
   header ('Content-Disposition: attachment; filename=' . date ( 'Ymd_His' ) . '_ack.csv');
   $this->data ['csv'] = $rows ; 
   echo $this->loadAndSetView ('csv'); 
   exit (); 
} 

You could read the .csv file into an array with file() , then create a new array, assigning the headers as the first entry, followed by writing all lines from the csv file to the new array. 您可以使用file()将.csv文件读入数组,然后创建一个新数组,将标头指定为第一个条目,然后将csv文件中的所有行写入新数组。 Finally, save your new CSV. 最后,保存新的CSV。

$oldcsv = file('sample.csv');
$csv = array();
$csv[] = "name,surname,address";
foreach($oldcsv as $v){
   $csv[] = $v;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM