2017-05-16 38 views
1

我需要重新格式化從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); 

?> 

回答

3

最簡單的方法是將整個數據集加載到一個數組中,並寫入一個結果CSV。如果數據量巨大並且不適合PHP允許的內存,這種方法只會造成麻煩。這是一個可以完成這項工作的示例腳本。它假設第一行是一個標題。

<?php 

$fp = fopen('yz-email.csv','r'); 
$hdr = false; 
$skip_header = true; 
$data = []; 
$contact_index = null; // Will take the last column index, if not set 

if ($fp) { 
    while(!feof($fp)) { 
    $row = fgetcsv($fp); 

    // Skip empty lines 
    if ((count($row) === 1) && is_null($row[0])) continue; 

    // Skip header 
    if (!$hdr) { 
     $hdr = true; 
     if (!isset($contact_index)) $contact_index = count($row)-1; 
     if ($skip_header) continue; 
    } 

    $email = strtolower(trim($row[0])); 
    if (isset($data[$email])) $data[$email][$contact_index].='|'.trim($row[$contact_index]); 
    else $data[$email] = array_map('trim',$row); 
    } 
    fclose($fp); 
} 

$fp = fopen('result.csv','w'); 
if ($fp) { 
    foreach($data as $row) { 
    fputcsv($fp,$row); 
    } 
    fclose($fp); 
} 
1

我剛剛開始了,請原諒我沒有使用您的確切代碼和從那裏建設。我添加了內聯文檔,因此應該很容易遵循。

<?php 
    $fname = "emails.csv";        //name of input file 
    $strOut = "";           //output string 
    $fileContents = file_get_contents($fname);    //read contents of file 
    $arrData = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $fileContents));;  //convert string into an array 
    $i=0;             //counter 
    $lastEmail = ""; 

    foreach($arrData as $row) {        //loop over the array 
    if(count($row) > 1) {         //for some reason, I was getting an extra empty array element, so I make sure it's a valid row here 
     if(compareEmails($row[0],$lastEmail)) {    //if different email, just append array 
     $strOut = $strOut . "|" .$row[10]; 
     } else { 
     $strOut .= "\r\n";        //ad the carriage return to the previous row, because we know it's a new email 
     $strOut = appendToString($row,$strOut);   //append to string 
     } 
     $i++; 
    } 
    $lastEmail = $row[0]; 
    } 

    function appendToString($arrIn,$strOut) {    //append the content onto the string 
    $strOut .= $arrIn[0] . ","; 
    $strOut .= $arrIn[1] . ","; 
    $strOut .= $arrIn[2] . ","; 
    $strOut .= $arrIn[3] . ","; 
    $strOut .= $arrIn[4] . ","; 
    $strOut .= $arrIn[5] . ","; 
    $strOut .= $arrIn[6] . ","; 
    $strOut .= $arrIn[7] . ","; 
    $strOut .= $arrIn[8] . ","; 
    $strOut .= $arrIn[9] . ","; 
    $strOut .= $arrIn[10]; 
    return $strOut; 
    } 
    function compareEmails($curEmail,$lastEmail) { 
    $curEmail = trim(str_replace('"', "", $curEmail));  //remove the quotes 
    $lastEmail = trim(str_replace('"', "", $lastEmail)); //remove the quotes 
    if($curEmail == $lastEmail) {       //compare them 
     return true; 
    } else { 
     return false; 
    } 
    } 
?> 
<pre> 
    <?php echo $strOut; ?> 
</pre>