Skip to content

Instantly share code, notes, and snippets.

@Momijinn
Last active September 14, 2015 12:02
Show Gist options
  • Save Momijinn/3d0e3ae3a4da05604c7e to your computer and use it in GitHub Desktop.
Save Momijinn/3d0e3ae3a4da05604c7e to your computer and use it in GitHub Desktop.
phpでcsvデータの解析
<?php
$filepath = "uriage.csv"; //csvファイル
$data = file_get_contents($filepath);
$data = mb_convert_encoding($data, 'UTF-8', 'sjis-win'); //shift-jisからutf-8へ変換
$temp = tmpfile();
$meta = stream_get_meta_data($temp);
$temp = tmpfile();
$meta = stream_get_meta_data($temp);
$file = new SplFileObject($meta['uri']);
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
$csv[$key1][$key2] = $value2;
}
}
print_r($csv);
?>
年度 売上 費用
2001 3425 123
2002 1232 453
2003 2344 564
2004 3453 452
2005 5673 686
2006 3453 565
2007 2352 453
2008 3453 455
2009 4565 123
2010 2345 435
2011 3454 345
2012 3534 234
2013 4564 567
2014 3453 232
2015 3245 123
2016 8957 234
@Momijinn
Copy link
Author

phpでcsvファイルを解析するプログラム

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment