Reputation: 3792
I would like to delete multiple lines from a CSV file.
I have an array (called $manual
) setup:
Array
(
[0] => A
[1] => B
[2] => C
...
My CSV looks like this:
A;3h2jhkj
B;ewrwer
C;fsfsdf
D;adsfsdf
E;sdfet
F;35435345f
My code to delete the lines are (not working):
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";
}
I realise why it's not working but can't get it to work.
What I want is all lines in the CSV that have a first column matching a value in the array to be deleted so the resulting CSV file from the above would yield:
D;adsfsdf
E;sdfet
F;35435345f
Upvotes: 0
Views: 178
Reputation: 5395
You don't need to iterate $manual
, just check if value in the $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);
Upvotes: 2