A
A
Aidar2017-12-16 13:04:15
PHP
Aidar, 2017-12-16 13:04:15

How to remove 3 first lines in csv file using PHP?

Hello! How to remove 3 first lines in csv file using PHP?
On the net I found only for one line:

<?php
    $row_number = 0;    //номер строки которую удаляем
    $file_out = file("file.csv"); // Считываем весь файл в массив     
    //записываем нужную строку  в файл
    file_put_contents("file2.csv", $file_out[$row_number], FILE_APPEND);     
    //удаляем записаную строчку
    unset($file_out[$row_number]);     
    //записали остачу в файл
    file_put_contents("file.csv", implode("", $file_out));
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2017-12-16
@dimonchik2013


do the line to delete three times , it will always be the first

A
Alexey, 2017-12-16
@AlexMaxTM

Deleting a line in your code is a side effect, its main purpose is to save the first line you need to a new file.
It's easier to delete rows.

$file_out = file("file.csv"); //читаем нужный файл
$file = array_splice($file, 3); //удаляем 3 строки
file_put_contents("file.csv", implode("", $file)); //сохраняем в этот же файл

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question