save in csv in php code example

Example: php modify csv file

$myfile = 'sample_csv.csv';

$fin = fopen($myfile, 'r');
$data = array();

   /***********
   * header row
   */
$data[] = fgetcsv($fin, 1000);
  /***********
   * data rows
   */
while ($line = fgetcsv($fin, 1000)) {
   echo join(', ', $line).'<br>';
   for($i = 4, $k = count($line); $i < $k; $i++) {
    if ($line[$i] < 1000) {
	    $line[$i] = 10000;
    }
   }
   $data[] = $line;
}

fclose($fin);
   /******************
   * reopen file and
   * write array to file
   */
$fout = fopen($myfile, 'w');
foreach ($data as $line) {
   fputcsv($fout, $line);
}
fclose($fout);

Tags:

Php Example