3

I want to delete current iterating row from CSV file, Suppose I have 5 rows (1,2,3,4,5) into my CSV file, I opened my file while iterating the rows one by one using foreach, when it comes to 3rd row then 3rd row should be deleted from CSV file and other 1,2,4,5 rows will be same while same iterration. I don't want to use like Skip the 3rd iterration and save it into another file, not like rename file name. So, Please anyone can help me how to do this in PHP?

Suppose, like SQL command to delete current row, is there anything in PHP?

Thanks.

Naveen
  • 41
  • 8
  • Read file into array, unset the row and write it back – SubjectDelta Sep 22 '17 at 11:53
  • Why are you against reading the file and writing a new file minus the rows you wanted to discard? You should avoid any memory issues this way when working with larger files. – X33 Sep 22 '17 at 11:55
  • Best way should be making a backup of the file, read it and write modifications into the first one. All happy! – SubjectDelta Sep 22 '17 at 11:59
  • @X33 Actually i'm using this into magento observers, observer is being loaded every times when i reload my product. So, according to my requirement, save data into new file will not work. – Naveen Sep 22 '17 at 12:03
  • @SubjectDelta Ok man, let me try your idea with some changes. Thanks – Naveen Sep 22 '17 at 12:08

1 Answers1

4

You will need to have permissions to write the file. If you remove a row, you will have to save the file. As you do not want to have any empty rows as well, you need to read the whole file.

I suggest to get the file content, and split it into a array by lines. With the explode function you can split the content by the line break, most likely "\n". So an array wich will contain each line of the csv file will be existing. Now you can simply remove the line from the array and create a string out of it, before saveing the changed content back to the csv file.

// get csv content into string variable
$csvContent = file_get_contents(__DIR__ . "/yourfile.csv");
// create array out of csv, limited by PHP_EOL wich determines the systems line break
// this means, every line in the csv file will be represended by an array key and its value
$csvArray = explode(PHP_EOL, $csvContent);

// unset the 3th csv line. 
unset($csvArray[2]); // keep in mind array starts at 0, so third line is array key 2 

// from here on you can manipulate the array $csvArray as you like. 
// add lines, remove lines or just use the given content for future operations. 
// you can also iterate over the array with: foreach ($csvArray as $line => $value) {}

// create string out of modified csv array
$csvContent = implode(PHP_EOL, $csvArray);
// save result back into the csv file
file_put_contents( __DIR__ . "/yourfile.csv", $csvContent);

Check implode/explode php function docs:

http://php.net/manual/en/function.implode.php

http://php.net/manual/en/function.explode.php

S.Gartmeier
  • 476
  • 5
  • 14
  • Better using [PHP_EOL](http://php.net/manual/en/reserved.constants.php#constant.php-eol) as end-of-line – SubjectDelta Sep 22 '17 at 12:11
  • @natheriel i want to delete my 3rd row before iterating the 4th and 5th row according to my requirements. So is it possible ? – Naveen Sep 22 '17 at 12:22
  • @Naveen with this method you do not itereate at all. You get the whole file, make an array with each line of the file as one array entry. Then delete the entry. You could add any iteration after the "unset" in my code. In this case you could iterate over an array where the 3rd line will allready be gone. At the end, use implode to create a string out of the array. Then write the string back to the file. – S.Gartmeier Sep 22 '17 at 12:26
  • @natheriel right, but my requirement is different, i'm getting 5 rows one by one, when iterating 1st line then appying some logic if row's data is according to my logic then i need it otherwise i wanna delete this row and don't want to fetch it next time from file, same with other rows. if i use your logic you are fetching all rows then deleting 3rd row. So like that i can't apply my logic on each row, Means i wanna delete that rows ASAP when it doesn't satisfy my logic. Reason is that i'm using Magento observers so my requirements are like that. – Naveen Sep 22 '17 at 12:44
  • File handing is not suitable for my problem, now i'm using db for this, Problem solved. Thanks. – Naveen Sep 23 '17 at 07:11