我需要重新格式化從DB導出的CSV文件以適合其他數據庫的條件。我根據「收件人」字段(電子郵件地址)訂購了CSV。我需要做的是,如果一個電子郵件地址重複,它應該用「|」標記最後一行「Concat」到上一行。作爲分隔符。它需要最終這樣看:使用PHP重新格式化CSV文件
recipient,lastSent,aftersunset,notes,fk_rty_id,confirmed,rty_id,rty_type,EnglishDate,,Concat
" [email protected]",1/21/17 5:00,1,,1,1,1,Yahrzeit,1/9/1991,01/09/1991,JOEL E. WEINGARTEN-01/09/1991
" [email protected]",6/29/16 5:00,0,,1,1,1,Yahrzeit,6/11/2015,06/11/2015,ANN SCHONBERG-06/11/2015|ALEXANDER SCHONBERG-12/26/2009
[email protected],3/24/17 5:00,0,,1,1,1,Yahrzeit,3/20/1985,03/20/1985,LEE I HOWARD-03/20/1985|IDA GALES-02/27/1990
這裏是我的CSV
recipient,lastSent,aftersunset,notes,fk_rty_id,confirmed,rty_id,rty_type,EnglishDate,,Concat
" [email protected]",1/21/17 5:00,1,,1,1,1,Yahrzeit,1/9/1991,01/09/1991,JOEL E. WEINGARTEN-01/09/1991
" [email protected]",6/29/16 5:00,0,,1,1,1,Yahrzeit,6/11/2015,06/11/2015,ANN SCHONBERG-06/11/2015
" [email protected]",1/6/17 5:00,0,,1,1,1,Yahrzeit,12/26/2009,12/26/2009,ALEXANDER SCHONBERG-12/26/2009
[email protected],3/24/17 5:00,0,,1,1,1,Yahrzeit,3/20/1985,03/20/1985,LEE I HOWARD-03/20/1985
[email protected],2/27/17 5:00,0,,1,1,1,Yahrzeit,2/27/1990,02/27/1990,IDA GALES-02/27/1990
這裏是PHP代碼,我到目前爲止有:
<?php
$file = fopen("yz-email.csv","r");
while(! feof($file))
{
$data = fgetcsv($file);
$num = count($data);
$concat = $data[22];
if ($concat != $newConcat) {
/*for ($c=0; $c<$num;$c++) {
print $data[$c].",";
} */
$newConcat = $concat;
} else {
array_push($data, $newConcat);
}
print "<pre>";
print_r($data);
print "</pre>";
}
fclose($file);
?>