0

still struggling with PHP and CSV file manipulation. I will try to ask this properly so I can get some help.

I have a CSV file with about 4000 lines/rows, and I wrote this code to create an array of the entire CSV, and pull the the LAST line of the CSV file out to use in my script. The code works to to all this wit success.

// CREATE ASSOCIATIVE ARRAY FROM LAST ROW OF CSV FILE
$csv = array();
if (FALSE !== $handle = fopen("Alabama-TEST.csv", "r"))
{
while (FALSE !== $row = fgetcsv($handle))
{
 $csv[] = $row; 
}
}
$new_csv = array();
foreach ($csv as $row)
{
$new_row = array();
for ($i = 0, $n = count($csv[0]); $i < $n; ++$i)
{
$new_row[$csv[0][$i]] = $row[$i]; 
}
$new_csv[] = $new_row;
}

The variable $new_row is the last row in the CSV and I am able to use the data fine. But when the script is finished running I want to delete this last row called $new_row.

Here is an example of my CSV file at the top view;

CITY        ADDRESS        STATE      NAME
Birmingham  123 Park St.   Alabama    Franky

So I just want to remove the last row and keep the header at the top, for the next time the script runs. I've been trying for 3 days solid trying to figure this out, so I'm hoping some kind person who KNOWS WHAT THEY ARE DOING can help.

Here is the code I have tried to remove the last row;

$inp = file('Alabama-TEST.csv');
$out = fopen('Alabama-TEST.csv','w');
or ($i=0;$i<count($inp)-1;$i++)
fwrite($out,$inp[$i]);
fclose($out);

Since I'm using a large file at around 4000 rows, is there a way to do this without using too much memory?

5
  • Since $n holds the the number of rows, why doesn't "$i < $n - 1" solve it? might it be that you didn't explain yourself correctly? Commented Nov 24, 2013 at 14:17
  • The code I showed works to get the variable $new_row perfectly, so nothing wrong with it - I'm looking for away to remove last row before the script runs again. I was assuming that by removing $new_row that would do it. I will add above the code I have tried to remove the last row. Commented Nov 24, 2013 at 14:23
  • Do you mean you want to write a new version of Alabama-TEST.csv without the last line? Commented Nov 24, 2013 at 14:24
  • @nrathaus - Yes, that is exactly what I am trying to do. Commented Nov 24, 2013 at 14:25
  • hashbangcode.com/blog/remove-last-line-file-php-478.html Perhaps this helps Commented Nov 24, 2013 at 14:27

1 Answer 1

2

You need to use array_pop on your array ($new_csv) and fputcsv to save the file:

array_pop($new_csv); // Removes the last element in the array
$out = fopen('Alabama-TEST-new.csv','w');
foreach ($new_csv as $row) { // sorry for writing it badly, try now
 fputcsv($out, $row);
}
fclose($out);

I don't have an environment to test this, sorry.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks - tried it and it ran with no errors - unfortunately it didn't delete the last row in the CSV - I think you are really close though because it did not mangle the CSV file in anyway.
Did you notice that I intentionally edited Alabama-TEST-new.csv and not the original file? :)
Sorry - I didn't use Alabama-TEST-new.csv - I'll try again
I did that to prevent overwriting your original file, did you use the same filename saw no modification?
Good one! That new file is perfect. Only issue is I want to run the script a second time using the new CSV, and again, again...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.