简体   繁体   中英

php CSV headers for columns

I want to write the columns for a generated CSV file into PHP headers. 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. Finally, save your new CSV.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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