Mike1020 Posted April 6, 2022 Share Posted April 6, 2022 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/314675-accessing-json-values/ Share on other sites More sharing options...
Mike1020 Posted April 6, 2022 Author Share Posted April 6, 2022 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 />"; } Quote Link to comment https://forums.phpfreaks.com/topic/314675-accessing-json-values/#findComment-1595083 Share on other sites More sharing options...
mac_gyver Posted April 6, 2022 Share Posted April 6, 2022 $response[0]['results'] is the array of data that you should be looping over in the foreach loop. $value['campaign']['name'] would be what to echo (don't create variables for nothing.) 1 Quote Link to comment https://forums.phpfreaks.com/topic/314675-accessing-json-values/#findComment-1595084 Share on other sites More sharing options...
Mike1020 Posted April 6, 2022 Author Share Posted April 6, 2022 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 />"; } Quote Link to comment https://forums.phpfreaks.com/topic/314675-accessing-json-values/#findComment-1595085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.