Hi, I have a question that you couldn't resolve. It turns out that I'm trying to go through a CSV
then delete the line that I choose and finally save the rest in it CSV
. The code that I have is not finished.
Example of the data content.vsv
Goggle.com,2019/03/25,2019/04/25,30
Facebook. Com,2019/03/25,2019/04/25,30
Vk.com,2019/03/25,2019/04/25,30
Vídeos.com,2019/03/25,2019/04/25,30
Yahoo.com,2019/03/25,2019/04/25,30
Twitte.com,2019/03/25,2019/04/25,30
The $remaining variable is random so it can be any line
$restante= 0;
$handle = fopen("datos.csv", "r");
while ($linea = fgetcsv($handle, 0, ",")) {
$enlace = @$linea[0];
$fecha1 = @$linea[1];
$fecha2 = @$linea[2];
$restante = @$linea[3];
if($restante == 2){
//
}elseif($restante == 0){
//elimina el enlace
$linea[0] = null;
$linea[1] = null;
$linea[2] = null;
$linea[3] = null;
fputcsv($handle, array($linea[0], $linea[1], $linea[2], $linea[3]));
}
$linea[3] = $restante;
}
fclose($handle);
As you can see the code is not finished because I can't find a way to remove the line and save the rest.
Update
$handle = fopen("datos.csv", "w");
while ($linea = fgetcsv($handle, 0, ",")) {
$enlace = @$linea[0];
$fecha1 = @$linea[1];
$fecha2 = @$linea[2];
$restante = @$linea[3];
$restan = 0;
if($restan != 0){
echo " no se borro ninguna linea";
fputcsv($handle, array($enlace,$fecha1,$fecha2,$restan));
}else{
echo "linea borrada: " .$enlace;
}
}
fclose($handle);
With this form he does not give me any message and datos.csv
he leaves it empty
The result I expect is that the line that is equal to $remaining is deleted from data.csv
I hope you can give me some idea. Kind regards.
you can do it like this:
in your case it is not necessary to use
fgetcsv
since you want to remove the whole line not to access the elements as such.