How To Delete The Top 100 Rows From a CSV File With PHP

前端 未结 2 1333
名媛妹妹
名媛妹妹 2021-01-28 17:34

I have a php script running on a regular basis that processes the top 100 rows of a CSV file. When it is done I want it to delete the processed rows from the CSV file.

I

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-28 18:17

    Here is php code for that:

    $input = explode("\n", file_get_contents("file.csv"));
    foreach ($input as $line) {
     // process all lines.
    }
    
    // This function removes first 100 elements.
    // More info:
    // http://php.net/manual/en/function.array-slice.php
    $output = array_slice($input, 100);
    file_put_contents("out.csv", implode("\n", $output));
    

    Note, if csv file contains header you have to remove first element from array $input.

提交回复
热议问题