[英]create each csv row to json file in php
我发现此 csv 行的代码为 object
$fp = fopen('test.csv','r') or die("**! can't open file\n\n");
$i = 0;
while($csv_line = fgetcsv($fp,1024)) {
$i++;
$json['json_'.$i]['id'] = $csv_line[0];
$json['json_'.$i]['product_id'] = $csv_line[1];
$json['json_'.$i]['title'] = $csv_line[2];
$json['json_'.$i]['outline'] = $csv_line[3];
}
$json['total_lines'] = $i;
print json_encode($json);
fclose($fp) or die("**! can't close file\n\n");
如何在 php 中将每个 csv 行创建为单独的 json 文件
简化的灵魂:
while($csv_line = fgetcsv($fp,1024)) {
$json = [
'id' => $csv_line[0],
'product_id' => $csv_line[1],
'title' => $csv_line[2],
'outline' => $csv_line[3],
];
// use any other naming approach that you want
file_put_contents('file_' . $csv_line[0] . '.json', json_encode($json));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.