Jump to content

Accessing JSON Values


Mike1020

Recommended Posts

Hi, I am getting JSON returned from a Google Ads API, and cannot seem to access the values. Trying to get the name of a campaign (Campaign #1-3). Tried using foreach loops mostly. Cannot figure this out. Any help would be greatly appreciated! This is a sample response.

Array
(
    [0] => Array
        (
            [results] => Array
                (
                    [0] => Array
                        (
                            [campaign] => Array
                                (
                                    [resourceName] => customers/12345/campaigns/12345
                                    [name] => Campaign #1
                                )

                        )

                    [1] => Array
                        (
                            [campaign] => Array
                                (
                                    [resourceName] => customers/12345/campaigns/12345
                                    [name] => Campaign #2
                                )

                        )

                    [2] => Array
                        (
                            [campaign] => Array
                                (
                                    [resourceName] => customers/12345/campaigns/12345
                                    [name] => Campaign #3
                                )

                        )

                )

            [fieldMask] => campaign.name
            [requestId] => er35tdgfjCWU-ddhaQ
        )

)

Link to comment
Share on other sites

A little more clarification. The JSON response is returned from CURL:
$cHTML = curl_exec($ch);
$response = json_decode($cHTML, true);

// Incorrect code, not returning values, but this is what I have so far

foreach($response as $value){
$campaignName = $value['results'][0]['campaign'][0]['name'];
echo "Campaign Name: $campaignName<br />";
}

Link to comment
Share on other sites

Thank you so much, I was completely stuck. You saved me hours or more...

Finished code:

$cHTML = curl_exec($ch);
$response = json_decode($cHTML, true);
echo "<br>CAMPAIGNS FOUND<hr><br>";

    foreach($response[0]['results'] as $value){
    echo $value['campaign']['name'];
    echo "<br />";
    }

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.