Fputcsv not writing correctly

I have a program that takes XML data and writes it to a .csv. I am changing it to write over a line if the data to write has an ID already in use.
fputcsv is taking an array that outputs like this:
Array ( [0] => 239547526463866 [1] => Sun May 1 15:02:48 GMT-0400 2011 [2] => 1 [3] => 4 [4] => [5] => )
and writes it like this:
239547526463866,Sun May 1 15:02:48 GMT-0400 2011,1,4
1,
(two lines with a 1 in the first column, unless it was the first line in the file, then the 1 gets placed in the second column of the second line).

First, I do not know why the array contains keys for 4 & 5 when they are empty. Second, I have absolutely no clue as to why it is writing two lines. This is the primary concern. What in my code is causing an extra line to be written? Third, I don’t have a lot of experience with PHP (I’m an ActionScript guy), so how does fputcsv end a line. In the .csv file, everything is on one line there is some kind of hidden end of line element (in notepad if I hit the right arrow to go through the data I have to hit it twice to move one space where an end of line occurs).

here is the pertinent code


$data = new SimpleXMLElement($HTTP_RAW_POST_DATA);
 $filename = "LongViewLog.csv";
 if(file_exists($filename))
 {
   $fh = fopen($filename, "r");
   $existing_columns = fgetcsv($fh);   $original_columns = $existing_columns;
   $num_original_columns = count($original_columns);
 
   fclose($fh);
   $timeColumn=array_search("time",$existing_columns);
   $fh=fopen($filename, "r");
   $idI=0;
   $currID=$data->id;
   $currTime=$data->time;
   $idIndex=-1;
   while (!feof($fh) ) {
    $line = fgetcsv($fh);
    if($currID==$line[0]){
     $idIndex=$idI;
     }
    $idI++;
   }
   fclose($fh);
   $idI=0;
 
   if($idIndex>=0){    
    $fh=fopen($filename,"r+");
 
    while (!feof($fh) ) {
     $line = fgetcsv($fh);
     if($idI==$idIndex){
      $line[$timeColumn]=(string)$currTime;
      $lineToWrite=$line;
 
     }
     $idI++;
    }
 
    fclose($fh);    
    $idI=0;
    $fh=fopen($filename,"r+");
 
    while (!feof($fh) ) {
     if($idI==$idIndex){
        fputcsv($fh,$lineToWrite);
     }
     $line2 = fgetcsv($fh);
     $idI++;
    }
 
    fclose($fh);
   } else {
//write data to new line, add headers if necessary
} else {
//create file, write headers and data
}