I'm sorry if this is newbie question but I don't understand how to access the [ids]
value in the JSON array via PHP.
Why is this not working?
$jsonResponse = json_decode($response,true);
print $jsonResponse[2]["ids"];
This is the JSON array:
Array
(
[0] => analytics#gaData
[1] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:123455&dimensions=ga:eventCategory,ga:eventAction,ga:eventLabel&metrics=ga:visits,ga:pageviews&start-date=2013-01-01&end-date=2020-01-01
[2] => Array
(
[start-date] => 2013-01-01
[end-date] => 2020-01-01
[ids] => ga:123455
[dimensions] => ga:eventCategory,ga:eventAction,ga:eventLabel
[metrics] => Array
(
[0] => ga:visits
[1] => ga:pageviews
)
[start-index] => 1
[max-results] => 1000
)
[3] => 1000
[4] => 9
There doesn't seem to be anything wrong with your code. Your code should be printing, what seems to me, the object ga:123455
if the code is executed. When printing this, this object will be converted to a string. PHP will do (string) ga:123455
(invoke the __toString()
method on ga:123455
) to convert an object to a string. If this method is absent, there must be a warning that ga:123455
cannot be converted to a string. __toString()
might also return an empty string.
I would suggest debugging it by doing print var_dump( $jsonResponse[2]["ids"] );
and print var_dump( (string) $jsonResponse[2]["ids"] );
.