1

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
}
2
  • Do you want to remove data in csv? Commented Dec 24, 2015 at 5:23
  • Consider i have 500 records , and also i have a condition Record $ProcessAfter ='R134'; R134 is a data present in 134th line so i need to remove until 134th line and process after 134th line Commented Dec 24, 2015 at 5:25

1 Answer 1

1
<?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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.