str_getcsv関数を使ってCSVファイルをパースし配列に格納するPHPサンプルコードを以下に紹介します。
htmlinsert(): The given local file does not exist or is not readable.
php.netのURLです。
http://jp1.php.net/manual/ja/function.str-getcsv.php
CSVファイルとして以下のファイルを用意しました。
php,python,perl ruby,java,c++ c#,vb,delphi
<?php $ary = array(); $h = fopen("data.csv", "r"); if ($h == FALSE) { print "File not found.\n"; } else { while(!feof($h)) { $l = fgets($h); $ary[] = str_getcsv($l); } } if ($h != FALSE) { fclose($h); } print_r($ary); ?>
本スクリプトと同じ場所にdata.csvを配置して実行します。
配列の中に配列になったCSV要素が格納されているのがprint_r関数により確認することができます。
$ php getcsv.php Array ( [0] => Array ( [0] => php [1] => python [2] => perl ) [1] => Array ( [0] => ruby [1] => java [2] => c++ ) [2] => Array ( [0] => c# [1] => vb [2] => delphi ) [3] => Array ( [0] => ) )
以上、CSVファイルを配列(配列内に配列)に格納するサンプルコードでした。
htmlinsert(): The given local file does not exist or is not readable.