PHP - write to CSV issue

91 views Asked by At

Read from a csv file and pick certain columns and write again to another csv file. Everything fine except that the last column is always get wrap around with a pair of double quotes and the last double quotes is in a new line, which is strange. Below is my code and the output

if( file_exists($file_path) && filesize($file_path) > 0){
      $counter = 0;
      if(false !== ($read_file = fopen($file_path,'r')) ){
        $output_file = fopen($file_output,'w');
        while(false !== ($data = fgetcsv($read_file))){
          $outputData = array($data[1], $data[6], $data[19]);
          fputcsv($output_file, $outputData,',');
        }

      }
      fclose($read_file);
      fclose($output_file);
      unset($outputData);

    }

Output: data1,data6,"data9
 "

Yes, so the last double quotes is in the newline and the last column is wrapped with a double quotes regards of what the data type is.

Thank you very much

0

There are 0 answers