1

I tried a lot of things, but none of them seems to work properly... This is the csv file: file csv

and this is my code:

                $file_handle = fopen('file.csv', 'r+'); 
                $positionID = 47901392; 
                while ($data = fgetcsv ($file_handle, 1000, ";")) {
            
                    $prod_id  =  $data[0];

                    if($prod_id ==$positionID){
                        echo "prod_id $prod_id ";
                        echo "I have to delete row";
                    }
                    
                    
                }

I have to delete row with specific $positionID.

1 Answer 1

1

Open another file to hold the new file with the lines deleted. Then have your loop write everything that isn't deleted to that file.

$file_handle = fopen('file.csv', 'r');
$out_handle = fopen('new_file.csv', 'w');
$positionID = 47901392; 
while ($data = fgetcsv ($file_handle, 1000, ";")) {
    $prod_id  =  $data[0];

    if($prod_id ==$positionID){
        echo "prod_id $prod_id ";
        echo "I have to delete row";
    } else {
        fputcsv($out_handle, $data);
    }
}
fclose($file_handle);
fclose($out_handle);
rename('new_file.csv', 'file.csv');
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.