0

我有一个数组:

$remove_rownumbers = array(1, 4, 7, 9, 2);

这些是行号,我希望从 PHP 中加载的 CSV 中删除。

现在我想通过这样的计数器和 in_array if 语句遍历行:

$row = 1;
if (($handle = fopen($CSV_FILE, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1500, ",")) !== FALSE) {
       if(in_array($row, $remove_rownumbers) == false) // if its not in the remove array, keep it
       {
         // (...) the output part, where im echoing out each row by foreach($data ...)
       }
       $row++;
    }
}

但是我怎样才能得到现在输出的回声并将其再次正确存储在 CSV 中呢?这样,数组中的行号就不会出现在这个新的行号中

我发现它的开始是这样的:

header('Content-Encoding: UTF-8');
header('Content-type: application/ms-excel; charset=utf-8');
header('Content-Disposition: attachment; filename=output.csv');
echo "\xEF\xBB\xBF"; 
$fp = fopen("php://output", "w");
4

1 回答 1

0

您需要 fopen() 一个新的 CSV 文件并获取句柄,但这很简单:

$row = 1;

if ( ($handle = fopen($CSV_FILE, "r")) !== FALSE ) {

    while ( ($data = fgetcsv($handle, 1500, ",")) !== FALSE ) {

        if ( in_array($row, $remove_rownumbers) == false ) {
            fputcsv($newFile, $data);
        }
        $row++;
    }
}

我忘记了你想在哪里下载它。使用重定向标头到新文件。

header("Location: http://www.example.com/newfile.csv");
于 2012-07-04T16:33:38.427 回答