php - Getting data from Yelp API

611

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>';
print_r($response);
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] => http://s3-media2.ak.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png
                    [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?

869

Answer

Solution:

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

businesses is a property, not an array element.

Everything belowstdClass 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 parametertrue:

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

Then the values would be in:

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

People are also looking for solutions to the problem: php - Creating Custom Latest Post Shortcode Including Featured image

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.