2

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": ""
        }]
    }]
}
3
  • Can you post a sample of the JSON data please?... Commented Aug 14, 2017 at 20:26
  • 2
    Do a print_r($decoded); so we can see what $decoded actually contains. We can't guess. Commented Aug 14, 2017 at 20:26
  • Hey @War10ck and Magnus I've added a small portion of the JSON Commented Aug 14, 2017 at 20:37

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

4 Comments

array_column expects parameter 1 to be an array but null is given
But I used this decode function: $decoded = json_decode($json_file, true);
Hey thanks man I play around with what was in the foreach loop but I managed to figure it out. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.