douwengzao5790 2017-08-14 20:24
浏览 152
已采纳

使用PHP将JSON从URL转换为CSV

When I run the code below it results in the first row in a long list of columns containing only the text Array.

I've used $decoded = json_decode($json_file, true); to indicate that I want arrays instead of objects so I'm not sure what the issue is.

Should I be using bracket or arrow notation somewhere to specify that I want the array values?

$json_file = file_get_contents('https://fakeurl.com&contentType=json', false);

$decoded = json_decode($json_file, true);

$fp = fopen('output.csv', 'w');
foreach($decoded as $comment) {
    fputcsv($fp, $comment);
}
fclose($fp);

Here is a small snippet of the JSON. It looks like this all the way through:

{
    "rows": [{
        "columns": [{
            "name": "clientId",
            "value": "1839",
            "type": "xs:int",
            "format": ""
        }, {
            "name": "campaignId",
            "value": "25646",
            "type": "xs:int",
            "format": ""
        }, {
            "name": "campaignStatus",
            "value": "Live",
            "type": "xs:string",
            "format": ""
        }, {
            "name": "campaignName",
            "value": "Template Donation Litle",
            "type": "xs:string",
            "format": ""
        }, {
            "name": "campaignExportName",
            "value": "Template Donation Litle",
            "type": "xs:string",
            "format": ""
        }, {
            "name": "description",
            "value": "/donate/template/Litle",
            "type": "xs:string",
            "format": ""
        }]
    }, {
        "columns": [{
            "name": "clientId",
            "value": "1839",
            "type": "xs:int",
            "format": ""
        }, {
            "name": "campaignId",
            "value": "25812",
            "type": "xs:int",
            "format": ""
        }, {
            "name": "campaignStatus",
            "value": "Live",
            "type": "xs:string",
            "format": ""
        }, {
            "name": "campaignName",
            "value": "Monthly Only",
            "type": "xs:string",
            "format": ""
        }, {
            "name": "campaignExportName",
            "value": "Monthly Only",
            "type": "xs:string",
            "format": ""
        }, {
            "name": "description",
            "value": "A donation receipt will be emailed to the address you submitted. This donation is tax-deductible to the fullest extent of the law.",
            "type": "xs:string",
            "format": ""
        }]
    }]
}
  • 写回答

1条回答 默认 最新

  • drj58429 2017-08-14 20:38
    关注

    Your array is much deeper and the values need to be extracted:

    foreach($decoded['rows'] as $row) {
        $values = array_column($row['columns'], 'value');
        fputcsv($fp, $values);
    }
    

    If you want the names/headers in the first row then you need to do this before the loop:

    $headers = array_column($decoded['rows'][0]['columns'], 'name');
    fputcsv($fp, $headers);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!