Q:

PHP Loop over txt file delete line after output

I have a php script where I am looping over all the lines in a text file.  How can delete the line after it was read, so by the end of the script all of the lines which have been output will be deleted from the file...
$file = $dataDoc['file'];
if(is_file($file)){
     $contents = file($file);
     foreach($contents as $line) {
        echo $line . " \n";
        // Delete line here and keep looping over the rest of the lines...
     }
}

A:

Simple solution can be:
$contents = file($file);
foreach($contents as &$line) {   // add `&` to modify source line
    echo $line . " \n";

    if (/*Some case is true*/) {
        $line = null;            // unset a line if needed
    }
}

// lines that are NULL will not be written to a file
$writeRes = file_put_contents($file, $contents);

