获得对象价值

时间:2015-11-07 19:57:56

标签: php api

我使用第三方API检索商家信息。该对象看起来有点像这样:

[businesses] => Array
    (
        [0] => stdClass Object
            (                    
                [name] => Business Name                    
                [location] => stdClass Object
                    (
                        [city] => Dallas
                        [display_address] => Array
                            (
                                [0] => 123 Yellow Rd 620 N

要获取我的商家名称:

$name = $response->businesses[0]->name;

我正在努力争取城市和地址。

$name = $response->businesses[0]['location']->city;

似乎不起作用......我缺少什么?

1 个答案:

答案 0 :(得分:3)

尝试以下 - location是一个Object,因此您应该可以使用 - >来引用它。符号

$city = $response->businesses[0]->location->city;

display_address

$display_address = $response->businesses[0]->location->display_address[0];