CSV以JSON用PHP? [英] CSV to JSON with PHP?
问题描述
我需要一个 CSV 文件转换为 JSON 使用PHP的服务器上。我使用这个脚本,它的工作原理:
函数csvToJSON($ CSV){
$行=爆炸(\ N,$ CSV);
$ i = 0;
$ LEN =计数($行);
$ JSON ={\ N。 ' 数据 : [';
的foreach($行作为$行){
$ COLS =爆炸(',',$行);
。$ JSON =\ N {\ N的;
$ JSON ='var0:'。$ COLS [0]\,\ N的;
$ JSON ='VAR1:'。$ COLS [1]\,\ N的;
$ JSON ='VAR2:'。$ COLS [2]\,\ N的;
$ JSON ='VAR3:'。$ COLS [3]\,\ N的;
$ JSON ='VAR4:'。$ COLS [4]\,\ N的;
$ JSON ='VAR5:'。$ COLS [5]\,\ N的;
$ JSON ='var6:'。$ COLS [6]\,\ N的;
$ JSON ='var7:'。$ COLS [7]\,\ N的;
$ JSON ='var8:'。$ COLS [8]\,\ N的;
$ JSON ='var9:'。$ COLS [9]\,\ N的;
$ JSON ='var10:;'$ COLS [10]。
。$ JSON =\ n}的;
如果(!$ I == $ LEN - 1){
。$ JSON =',';
}
$ I ++;
}
。$ JSON =\ n]的\ N};
返回$ JSON;
}
$ JSON = csvToJSON($ CSV);
$ JSON = preg_replace('/ [\ N] /','',$ JSON);
标题(内容类型:text / plain的);
标题(缓存控制:无缓存);
回声$ JSON;
在 $ CSV
变量是从卷曲请求返回的CSV内容引起的字符串。
我敢肯定这是不是最有效的PHP code这样做,因为我是一个初学者开发商和我的PHP知识是低的。 有没有使用PHP CSV转换成JSON一个更好的,更有效的方法?
在此先感谢。
注意我知道,我加入空格,然后删除它,我这样做,这样我就可以有回报可读的JSON通过删除行选项 $ JSON = preg_replace('/ [\ N] /','',$ JSON);
用于测试目的
修改感谢您的答复,根据他们新的code是这样的:
函数csvToJson($ CSV){
$行=爆炸(\ N,修剪($ CSV));
$ csvarr = array_map(函数($行){
$键=阵列('var0','var1的','VAR2','var3的','VAR4','VAR5','var6','var7','var8','var9','var10');
返回array_combine($键,str_getcsv($行));
} $行);
$ JSON = json_en code($ csvarr);
返回$ JSON;
}
$ JSON = csvToJson($ CSV);
标题(内容类型:应用程序/ JSON);
标题(缓存控制:无缓存);
回声$ JSON;
那么就有json_en code()函数,你应该使用,而不是建立在JSON输出自己。而且还有一个功能str_getcsv()来解析CSV:
$阵列= array_map(str_getcsv,爆炸(\ N,$ CSV));
打印json_en code($阵列);
如果您希望JSON输出举行命名字段然而,你必须调整$数组。
I need to convert a CSV file to JSON on the server using PHP. I am using this script which works:
function csvToJSON($csv) {
$rows = explode("\n", $csv);
$i = 0;
$len = count($rows);
$json = "{\n" . ' "data" : [';
foreach ($rows as $row) {
$cols = explode(',', $row);
$json .= "\n {\n";
$json .= ' "var0" : "' . $cols[0] . "\",\n";
$json .= ' "var1" : "' . $cols[1] . "\",\n";
$json .= ' "var2" : "' . $cols[2] . "\",\n";
$json .= ' "var3" : "' . $cols[3] . "\",\n";
$json .= ' "var4" : "' . $cols[4] . "\",\n";
$json .= ' "var5" : "' . $cols[5] . "\",\n";
$json .= ' "var6" : "' . $cols[6] . "\",\n";
$json .= ' "var7" : "' . $cols[7] . "\",\n";
$json .= ' "var8" : "' . $cols[8] . "\",\n";
$json .= ' "var9" : "' . $cols[9] . "\",\n";
$json .= ' "var10" : "' . $cols[10] . '"';
$json .= "\n }";
if ($i !== $len - 1) {
$json .= ',';
}
$i++;
}
$json .= "\n ]\n}";
return $json;
}
$json = csvToJSON($csv);
$json = preg_replace('/[ \n]/', '', $json);
header('Content-Type: text/plain');
header('Cache-Control: no-cache');
echo $json;
The $csv
variable is a string resulting from a cURL request which returns the CSV content.
I am sure this is not the most efficient PHP code to do it because I am a beginner developer and my knowledge of PHP is low. Is there a better, more efficient way to convert CSV to JSON using PHP?
Thanks in advance.
Note. I am aware that I am adding whitespace and then removing it, I do this so I can have the option to return "readable" JSON by removing the line $json = preg_replace('/[ \n]/', '', $json);
for testing purposes.
Edit. Thanks for your replies, based on them the new code is like this:
function csvToJson($csv) {
$rows = explode("\n", trim($csv));
$csvarr = array_map(function ($row) {
$keys = array('var0','var1','var2','var3','var4','var5','var6','var7','var8','var9','var10');
return array_combine($keys, str_getcsv($row));
}, $rows);
$json = json_encode($csvarr);
return $json;
}
$json = csvToJson($csv);
header('Content-Type: application/json');
header('Cache-Control: no-cache');
echo $json;
Well there is the json_encode() function, which you should use rather than building up the JSON output yourself. And there is also a function str_getcsv() for parsing CSV:
$array = array_map("str_getcsv", explode("\n", $csv));
print json_encode($array);
You must however adapt the $array if you want the JSON output to hold named fields.
这篇关于CSV以JSON用PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!