Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提取JSON值并添加到现有CSV中的新列_Php_Json_Csv - Fatal编程技术网

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

Php 提取JSON值并添加到现有CSV中的新列,php,json,csv,Php,Json,Csv,我试图从JSON(如下)中获取值,并将它们插入到现有CSV的新列中。对于我当前的代码(也在下面),添加到新行的所有内容都是每行上的单词“Array”。如何调整代码使其正常工作?还有,有没有办法为我正在创建的新列设置标题名 谢谢,请看我下面的代码,如果您有任何问题,请告诉我 JSON blob: { "test":{"12345":"98765","56789":"54321"}, "control":{"99999":"987651","88888":"987652","22222":"98

我试图从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(根据):

$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=另一种简单快捷的方法是:

$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建议的方法)后应该实现的第一种方法。。只是想把它扔出去


Dins

预期结果.csv文件应该是什么样子?顺便说一句,你已经发布了两次这个问题。为什么不只是增强旧帖子呢?我只是添加了生成的CSV文件应该如何处理我的问题。我决定不加强最后一篇文章,因为它被标记为重复(我认为不公平)。这篇文章有更多的信息。然后你应该删除旧的帖子。我在这一行得到一个解析错误
$json\u values=array\u values($json['control'])。错误是“Parse Error:Parse Error,第8行foo.php中应为'T_STRING'或'T_VARIABLE'或'T_NUM_STRING'”,可能您未能正确复制/粘贴它。代码可以工作(现在测试了两次),这很奇怪。我想我复制/粘贴不正确。谢谢你的帮助!NP有时会发生这种情况,因为您选择/复制了隐藏字符或其他。。。。。
$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);