我有一个数组:
$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");
你需要 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");