LoginSignup
0
1

More than 3 years have passed since last update.

csvから取得した値を1件ずつ処理して出力

Posted at
<?php 
    $lines = file('readfile.csv');

    $cnt = count($lines);//レコード数
    $i = 0;

    $fp = fopen('php://temp', 'w');//一時ファイル

    while($i<$cnt){
        $data = explode(',',$lines[$i]);

        //ここで色々な処理

        mb_convert_variables('SJIS-win', 'UTF-8', $data);//文字コード変換
        fputcsv($fp, array_values($data));//一時ファイルに1件書き出し

        ++$i;
    }

    rewind($fp);//ファイルポインタを先頭に戻す
    file_put_contents("outputfile.csv", stream_get_contents($fp));//出力
    fclose($fp);
 ?>

参考
PHPでCSVファイルの書き込みを行う方法
PHPで容量の大きいデータをCSV出力するときに工夫したこと

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1