Created
April 13, 2019 21:00
-
-
Save SnowCait/072de90146899ccd528df40cfa644bf8 to your computer and use it in GitHub Desktop.
CSV to JSON convert on PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $path = '/path/to/example.csv'; | |
| $name = basename($path, '.csv'); | |
| // https://www.php.net/manual/ja/function.str-getcsv.php#114764 | |
| $csv = array_map('str_getcsv', file($path)); | |
| $header = array_shift($csv); | |
| $data = array_map(function ($row) use ($header) { | |
| return array_combine($header, array_map(function ($value) { | |
| if ($value === 'TRUE') { return true; } | |
| if ($value === 'FALSE') { return false; } | |
| return $value; | |
| }, $row)); | |
| }, $csv); | |
| file_put_contents("/path/to/{$name}.json", json_encode($data)); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ファイル全体を読み込むので注意。巨大ファイルは
fgetcsvか goodby/csvで。TRUE,FALSEを特別扱いして bool 値に変換。