title: PHP to remove last line of CSV file
body: <p>still struggling with PHP and CSV file manipulation. I will try to ask this properly so I can get some help.</p>
  
  <p>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.</p>
  
  <pre><code>// 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 &lt; $n; ++$i)
  {
  $new_row[$csv[0][$i]] = $row[$i]; 
  }
  $new_csv[] = $new_row;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Here is an example of my CSV file at the top view;</p>
  
  <pre><code>CITY        ADDRESS        STATE      NAME
  Birmingham  123 Park St.   Alabama    Franky
  </code></pre>
  
  <p>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.</p>
  
  <p>Here is the code I have tried to remove the last row;</p>
  
  <pre><code>$inp = file('Alabama-TEST.csv');
  $out = fopen('Alabama-TEST.csv','w');
  or ($i=0;$i&lt;count($inp)-1;$i++)
  fwrite($out,$inp[$i]);
  fclose($out);
  </code></pre>
  
  <p>Since I'm using a large file at around 4000 rows, is there a way to do this without using too much memory?</p>