2017-05-16 14 views
1

DBからエクスポートされているCSVファイルを別のDBの条件を満たすように再フォーマットする必要があります。私は「受信者」フィールド(電子メールアドレス)に基づいて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の許容メモリに収まらない場合にのみ問題を引き起こす可能性があります。次に、そのジョブを実行するサンプルスクリプトを示します。最初の行がヘッダーであると仮定します。

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> 
関連する問題