-1

I am downloading csv reports from doubleclick for advertisers. My client needs to save the the csv to database tables. Before saving the csv to database, I need to delete few rows from top and grand total row from bottom. Is there any function in PHP using which I can delete rows which I specify from csv files?

Drupal Sinha
  • 27
  • 1
  • 3
  • do you think there's any way you could make your question less specific? – davogotland Jan 07 '12 at 05:20
  • possible duplicate of [How do I open a file from line X to line Y in PHP?](http://stackoverflow.com/questions/514673/how-do-i-open-a-file-from-line-x-to-line-y-in-php) – Gordon Jan 07 '12 at 11:16
  • possible duplicate of [PHP Read CSV and filter by date](http://stackoverflow.com/questions/2127775/php-read-csv-and-filter-by-date) – Gordon Jan 07 '12 at 11:17
  • possible duplicate of [How to extraxt data from csv file](http://stackoverflow.com/questions/2805427/how-to-extract-data-from-csv-file-in-php/2805486#2805486) – Gordon Jan 07 '12 at 11:18

1 Answers1

0

There is no specific function for that, but there are several ways to accomplish it.

If you have enough RAM to hold the entire file in memory at once, you could convert the file to an array with:

$data=explode("\n",file_get_contents("path/to/csv_file");

Then, to get rid of the totals at the bottom, you can just unset the last row with something like:

unset($data[sizeof($data)-1);

The way that works is that each line in the array is numbered, but starting from zero. The "sizeof" would be the number of rows, but since we started counting with zero, it would be one too many.

To remove some rows from the top, you could

unset($data[0]); and unset($data[1]);

Then use the foreach() function to loop through the rows and insert into the database:

foreach($data as $line){
/// parse your $line 
/// Insert row into database
}
Gordon
  • 312,688
  • 75
  • 539
  • 559
user984869
  • 432
  • 2
  • 8