LoginSignup
0
0

CSVファイルを読み込む

Posted at

概要

CSVファイルを一行づつ読み込んで表示する

手順

  1. 読み込みたいファイルを開く
  2. データを1行づつ読み込む
  3. ファイルを閉じる

getData()を呼び出す形で実行することを想定。

function getData(){

    //ファイルを開く
    $csvfile = fopen(__DIR__.'/../data/data.csv','r');

    //1行づつ読み込む
    while($line = fgetcsv($csvfile)){

        //読み込んだ1行づつを表示していく
        var_dump($line);
    }

    //ファイルを閉じる
    fclose($csvfile);
}

ファイルをfopen(ファイルパス)で開いて$scvfileに格納。
fgetcsv()でCSVファイルを1行づつ読み込み、$lineに入れ込み、なくなるまでwhile文で回す。
whileの中でvar_dumpで$lineの中身を1行づつ表示し、最後にfclose()で対象ファイルを閉じる。

0
0
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
0