使用PHP重新格式化CSV文件

使用PHP重新格式化CSV文件,php,csv,fgetcsv,Php,Csv,Fgetcsv,我需要重新格式化从数据库导出的CSV文件,以符合另一个数据库的标准。我根据“收件人”字段(电子邮件地址)订购了CSV。我需要做的是,如果电子邮件地址重复,它应该在前一行的最后一行“Concat”上标记“|”作为分隔符。它需要以这样的方式结束: recipient,lastSent,aftersunset,notes,fk_rty_id,confirmed,rty_id,rty_type,EnglishDate,,Concat " bheller@email.org",1/21/17 5:00

我需要重新格式化从数据库导出的CSV文件,以符合另一个数据库的标准。我根据“收件人”字段(电子邮件地址)订购了CSV。我需要做的是,如果电子邮件地址重复,它应该在前一行的最后一行“Concat”上标记“|”作为分隔符。它需要以这样的方式结束:

recipient,lastSent,aftersunset,notes,fk_rty_id,confirmed,rty_id,rty_type,EnglishDate,,Concat
"   bheller@email.org",1/21/17 5:00,1,,1,1,1,Yahrzeit,1/9/1991,01/09/1991,JOEL E. WEINGARTEN-01/09/1991
"   123456@email.com",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
1234benn@email.net,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
"   bheller@email.org",1/21/17 5:00,1,,1,1,1,Yahrzeit,1/9/1991,01/09/1991,JOEL E. WEINGARTEN-01/09/1991
"   123456@email.com",6/29/16 5:00,0,,1,1,1,Yahrzeit,6/11/2015,06/11/2015,ANN SCHONBERG-06/11/2015
"   123456@email.com",1/6/17 5:00,0,,1,1,1,Yahrzeit,12/26/2009,12/26/2009,ALEXANDER SCHONBERG-12/26/2009
1234benn@email.net,3/24/17 5:00,0,,1,1,1,Yahrzeit,3/20/1985,03/20/1985,LEE I HOWARD-03/20/1985
1234benn@email.net,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);

?>

最简单的方法是将整个数据集加载到一个数组中,然后写入结果CSV。这种方法只能在数据量巨大且无法放入PHP允许的内存时才会造成问题。下面是一个执行此任务的示例脚本。它假定第一行是标题

<?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>

最简单的方法是将整个数据集加载到一个数组中,然后将数据写入结果CSV中。这种方法只能在数据量巨大且无法放入PHP允许的内存时才会造成问题。下面是一个执行此任务的示例脚本。它假定第一行是标题

<?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>

我刚刚重新开始,请原谅我没有使用您的确切代码和建筑。我添加了内联文档,所以应该很容易理解


我刚刚重新开始,请原谅我没有使用您的确切代码和建筑。我添加了内联文档,所以应该很容易理解