您好,欢迎访问一九零五行业门户网

php csv如何转json

php csv转json的方法:首先创建一个php示例文件;然后通过“csvjson”方法读取csv文件转换成json;最后写入新的文件即可。
推荐:《php视频教程》 
php读取csv文件转换成json并写入新的文件
没什么好说的,直接上代码。
<?phpfunction csvjson() {// data.csv 是你的csv文件 $lines = array_map('str_getcsv', file('data.csv'));; $result = array();$headers; if (count($lines) > 0) {$headers = $lines[0];} for($i=1; $i<count($lines); $i++) { $obj = $lines[$i]; $result[] = array_combine($headers, $obj); } return json_encode($result, json_pretty_print);} // testdata.js 为要写入的文件$myfile = fopen("testdata.js", "w") or die("unable to open file!");fwrite($myfile, csvjson());fclose($myfile); // printf($content); ?>
以上就是php csv如何转json的详细内容。
其它类似信息

推荐信息