Skip to content

Instantly share code, notes, and snippets.

@bzz0217
Last active May 3, 2016 13:44
Show Gist options
  • Save bzz0217/5c249a1e8121956fa0a03a0185a2f60d to your computer and use it in GitHub Desktop.
Save bzz0217/5c249a1e8121956fa0a03a0185a2f60d to your computer and use it in GitHub Desktop.
CSVを取得し配列に変換
<?php
class get_csv{
public $file;
public $from_encoding;
/*
* @param string filepath ファイルパス
* @param string from_encoding CSVファイルの文字コード
*/
public function __construct($filepath, $from_encoding = "SJIS"){
$this->file = new SplFileObject($filepath);
$this->file->setFlags(SplFileObject::READ_CSV);
$this->from_encoding = $from_encoding;
}
/*
* CSVを配列に変換
* @return array records CSV配列
*/
public function csv_array_convert(){
foreach ($this->file as $line){
if(!is_null($line[0])){
mb_convert_variables("UTF-8", $this->from_encoding, $line);
$records[] = $line;
}
}
return $records;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment