[英]PHP: Delete multiple lines from CSV
我想從CSV文件中刪除多行。
我有一個數組(稱為$manual
)設置:
Array
(
[0] => A
[1] => B
[2] => C
...
我的CSV如下所示:
A;3h2jhkj
B;ewrwer
C;fsfsdf
D;adsfsdf
E;sdfet
F;35435345f
我刪除行的代碼(不起作用):
foreach ($manual as $filename) {
$table = fopen('logs/error.log','r');
$temp_table = fopen('logs/error_temp.log','wa+');
while (($data = fgetcsv($table, 2048, ";")) !== FALSE) {
if($data[0] == $filename){
continue;
}
fputcsv($temp_table,$data);
}
fclose($table);
fclose($temp_table);
//$done = "YES";
}
我知道為什么它不起作用但無法正常工作。
我想要的是CSV中所有第一行都與要刪除的數組中的值匹配的行,因此從上面生成的CSV文件將產生:
D;adsfsdf
E;sdfet
F;35435345f
您不需要迭代$manual
,只需檢查$manual
值。
$table = fopen('logs/error.log','r');
$temp_table = fopen('logs/error_temp.log','wa+');
while (($data = fgetcsv($table, 2048, ";")) !== FALSE) {
if(in_array($data[0], $manual, true)){
continue;
}
fputcsv($temp_table,$data);
}
fclose($table);
fclose($temp_table);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.