santa santa - 4 months ago 36x
PHP Question

Getting data from Yelp API

I'm starting to look into Yelp API. When I send a search request I get a data returned in an array $response. So If I output it like this

echo '<pre>';
echo '</pre>';

I see results in the following format

stdClass Object
[message] => stdClass Object
[text] => OK
[code] => 0
[version] => 1.1.1

[businesses] => Array
[0] => stdClass Object
[rating_img_url] =>
[country_code] => US

So, let's say I want to get the country code, shouldn't I be able to get it with something like?

echo $response['businesses'][0]->country_code;

I'm not getting any results. What am I missing?

echo $response->businesses[0]->country_code;

businesses is a property, not an array element.

Everything below stdClass Object are properties.

Everything below => Array are Array Elements.

Let me guess, $response = json_decode(...); ?

You can tell this function to return associative arrays instead of objects by putting up the second parameter true:

$response = json_decode(..., true);

Then the values would be in:

echo $response['businesses'][0]['country_code'];