douzizang7783 2014-05-22 15:21
浏览 114
已采纳

从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>';
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?

  • 写回答

1条回答 默认 最新

  • du1108 2014-05-22 15:24
    关注
    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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?