user5621153
user5621153

Reputation:

PHP to remove data from Csv file according to matching condition

The csv file contains

ID,Record
 1,R1
 2,R2
 3,R3
 4,R4
 5,R5

Consider the above is my csv file data and i have a variable with $ProcessAfter ='R3'; Then it should remove the first three data. The Below is my file reading code

while(!feof($file)) {
   fgetcsv($file,0,$fieldseparator); //To read each line
}

Upvotes: 1

Views: 85

Answers (1)

Shashank Shekhar
Shashank Shekhar

Reputation: 75

<?php
$ProcessAfter = 'R3';
$flag  = 0;
$data = fopen("file_name.csv", "w+")
while (($field = fgetcsv($data,",")) !== FALSE) {
    if($field[1] == $ProcessAfter){
            $flag = 1;
    }
    if($flag == 1){
        file_put_contents("sorted_file",$field,FILE_APPEND);
    }
}

?>

Now use the new file for processing as it will have the sorted data

Upvotes: 1

Related Questions