提取JSON值并添加到现有CSV中的新列中


Pull JSON values and add to new column in existing CSV

我正试图从JSON(下面)获取值,并将它们插入现有CSV的新列中。对于我当前的代码(也在下面),添加到新行的只是每行上的单词"Array"。我如何调整我的代码,使其正常工作?此外,是否有一种方法让我设置我正在创建的新列的标题名称?

谢谢你,请看看我下面的代码,如果你有任何问题请告诉我。

JSON blob:

{
 "test":{"12345":"98765","56789":"54321"},
 "control":{"99999":"987651","88888":"987652","22222":"987653","27644":"987655"}
}
当前CSV

:

userid,foo_date,bar,baz,qux,country
"12345","2013-03-14","1.72500","1","1055.74","UK"
"38726","2013-03-14",'N,"1","3430.07","UK"
"85127","2013-03-14",'N,"0","635.25","US"
"16984","2013-03-14",'N,"1","5233.09","US"

当前PHP(根据如何使用PHP向CSV添加列):

$json = json_decode($s, true);
$json_values = array_values($json['control']);
$newCsvData = array();
if (($handle = fopen("testgroup.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $data[] = $json_values;
        $newCsvData[] = $data;
    }
    fclose($handle);
}
$handle = fopen('testgroup.csv', 'w');
foreach ($newCsvData as $line) {
   fputcsv($handle, $line);
}
fclose($handle);

$s = JSON blob。

生成的CSV应该如下所示:

userid,foo_date,bar,baz,qux,country,extra
"12345","2013-03-14","1.72500","1","1055.74","UK","987651"
"38726","2013-03-14",'N,"1","3430.07","UK","987652"
"85127","2013-03-14",'N,"0","635.25","US","987653"
"16984","2013-03-14",'N,"1","5233.09","US","987655"

试试这个(已测试):

$data = <<<EOF
{
 "test":{"12345":"98765","56789":"54321"},
 "control":{"99999":"987651","88888":"987652","22222":"987653","27644":"987655"}
}
EOF;
$json = json_decode($data, true);
$json_values = array_values($json['control']);
// add column header to the start of the array
array_unshift($json_values, 'extra');
// open the file in read write mode.
$fd = fopen('testgroup.csv', 'r+');
// add the last field for each record
$records = array();
while($record = fgetcsv($fd)) {
    $record []= array_shift($json_values);
    $records []= $record;
}
// clear file and set seek to start
ftruncate($fd, 0); 
fseek($fd, 0); 
// rewrite file
foreach($records as $record) {
   fputcsv($fd, $record);
}
fclose($fd);

另一种简单快捷的方法是:

$json = json_decode($s, true);
$json_values = array_values($json['control']);
$i = 0;
if (($handle = fopen("csvFile.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
       //seperate your header data
        $i++;
        if($i==1)
            $header = $data;
        else
            $datas[] = $data; //all the data
        $i++;
    }
    fclose($handle);
}
//adding the json now to the data based on the test value
// assuming that your json data length will be same as number of rows in csv
//you can modify the code easily to make it dynamic
$j=0;
foreach($datas as &$d){
    array_push($d, $json_values[$j]);
    $j++;       
}
//than append data in the file
$handle = fopen('testgroup.csv', 'r+');
//put the header inside the csv
fputcsv($handle, $header);
foreach ($datas as $line) {
   fputcsv($handle, $line);
}
fclose($handle);

这是你应该实现的第一种方式,一旦你能够找到一个更好的方法来做到这一点,就像@hek2mgl建议的那样。只是想把它扔在那里....

din