使用列分隔符将CSV文件转换为JSON

时间:2015-09-04 08:49:22

标签: php json csv

我有一个CSV文件(my_data.csv),我想将其转换为JSON对象。 用列“;”它给了我错误的结果,逗号“,”它工作正常。 但我不想编辑csv文件。我不想使用str_replace因为csv文件很大而且需要更长的时间。 如何更改转换函数以生成正确的JSON格式? 这是我的CSV文件:

"Timestamp";"Longitude";"Latitude";"Client";"Price"
"2015-08-01 05:10:13";10.714069;48.031952;"test1";17.2
"2015-08-01 05:10:13";10.714069;48.031952;"test2";17.2
"2015-08-01 05:10:13";10.714069;48.031952;"test3";17.2
"2015-08-01 05:10:13";10.714069;48.031952;"test4";17.2

这是我的PHP代码

$file="data/my_file.csv";
$csv= file_get_contents($file);
$rows = explode("\n", trim($csv));
$data = array_slice($rows, 1);
$keys = array_fill(0, count($data),$rows[0] );
$json = array_map(function ($row, $key) {
    return array_combine(str_getcsv($key), str_getcsv($row));
}, $data, $keys);
$data=json_encode($json);
echo $data;

我明白了:

    [{"Timestamp;\"Longitude\";\"Latitude\";\"Client\";\"Price\"":"2015-08-01 05:10:13;10.714069;48.031952;\"test1\";17.2"},
{"Timestamp;\"Longitude\";\"Latitude\";\"Client\";\"Price\"":"2015-08-01 05:10:13;10.714069;48.031952;\"test2\";17.2"},
{"Timestamp;\"Longitude\";\"Latitude\";\"Client\";\"Price\"":"2015-08-01 05:10:13;10.714069;48.031952;\"test3\";17.2"},
{"Timestamp;\"Longitude\";\"Latitude\";\"Client\";\"Price\"":"2015-08-01 05:10:13;10.714069;48.031952;\"test4\";17.2"}]

应该是这样的:

[{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test1","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test2","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test3","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test4","Price":"17.2"}]

2 个答案:

答案 0 :(得分:1)

你必须改变

return array_combine(str_getcsv($key), str_getcsv($row));

return array_combine(str_getcsv($key, ';'), str_getcsv($row, ';'));

因为参数$delimiter的默认值为',':

  

array str_getcsv(string $ input [, string $ delimiter ="," [,string   $ enclosure ='"' [,string $ escape =" \" ]]])

答案 1 :(得分:0)

替代解决方案:

<?php
$csvString = file_get_contents('/path/to/data.csv');
$csvRows = str_getcsv($csvString, "\n"); // split new lines, gives you rows
$rows = array();
foreach($csvRows as $row) {
    $rows[] = str_getcsv($row, ";"); // parse the rows
}

// remove the first row. its the CSV column heading line. keep keys for reuse.
$itemKeys = array_shift($rows);

// run over all rows and combine keys with values, to get a named array
foreach($rows as $rowKeys => $rowValues) {
    $array[] = array_combine($itemKeys, $rowValues);
}

echo json_encode($array);

结果:

[{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test1","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test2","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test3","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test4","Price":"17.2"}]