如何转置CSV数据

时间:2014-01-09 21:53:28

标签: php email csv

我是PHP的新手,并且已经设法通过使用我放在一起的各种代码来实现这一点。但是我需要帮助转置数据。

我得到的那一刻:

标题,姓,姓,多布,高 先生,弗雷德,埃文斯,01/06/79,是的

但我要求的是:

title,先生

forname,fred

姓,埃文斯

dob,01/06/79

很高,是的。

这是我的代码:

$message = "". 
"title: $title" . "\n" . 
"forename: $forename" . "\n" . 
"surname: $surname"; 
"DOB: $dob";
"high: $high";

//The Attachment 

$cr = "\n"; 
$data = "Title" . ',' . "Forename" . ',' . "Surname" . ',' . "DOB" . ',' . "High Blood Pressure" . $cr; 
$data .= "$title" . ',' . "$forename" . ',' . "$surname" . ',' . "$dob" . ',' . "$high" . $cr; 
$fp = fopen('file.csv','a'); 
fwrite($fp,$data); 
fclose($fp); 

$attachments[] = Array( 
'data' => $data, 
'name' => 'file.csv', 
'type' => 'application/vnd.ms-excel' 
); 

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您写入CSV的数据由您控制。目前你正在写这个:

$data = "Title" . ',' . "Forename" . ',' . "Surname" . ',' . "DOB" . ',' . "High Blood Pressure" . $cr; 
$data .= "$title" . ',' . "$forename" . ',' . "$surname" . ',' . "$dob" . ',' . "$high" . $cr; 

更改这些行,使其像这样:

$data = "Title," . $title . "\n";
$data .= "Forename," . $forename . "\n";
//...