[PHP] CSVデータの扱い

PHP
たま〜に、csvデータを扱うシステムに出会う。 そんな時のためのスニペット

csvを読み込んでParse

data.csv

a,b,c,d,e 11,22,33,44,55 a1,a2,a3,a4,a5 b1,b2,b3,b4,b5

sample.php

<?php $csv = array_map('str_getcsv', file('data.csv')); echo '<pre>'; print_r($csv);

結果

Array ( [0] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) [1] => Array ( [0] => 11 [1] => 22 [2] => 33 [3] => 44 [4] => 55 ) [2] => Array ( [0] => a1 [1] => a2 [2] => a3 [3] => a4 [4] => a5 ) [3] => Array ( [0] => b1 [1] => b2 [2] => b3 [3] => b4 [4] => b5 ) )

参考サイト

phpマニュアル : str_getcsv