我有以下数组,无法弄清楚如何获取数据:
Array (
[0] => stdClass Object (
[address_components] => Array (
[0] => stdClass Object (
[long_name] => 180
[short_name] => 180
[types] => Array ( [0] => street_number )
)
[1] => stdClass Object (
[long_name] => Campus Dr
[short_name] => Campus Dr
[types] => Array ( [0] => route )
)
[2] => stdClass Object (
[long_name] => Arcadia
[short_name] => Arcadia
[types] => Array (
[0] => locality
[1] => political
)
)
[3] => stdClass Object (
[long_name] => Los Angeles
[short_name] => Los Angeles
[types] => Array (
[0] => administrative_area_level_2
[1] => political
)
)
[4] => stdClass Object (
[long_name] => California
[short_name] => CA
[types] => Array (
[0] => administrative_area_level_1
[1] => political
)
)
[5] => stdClass Object (
[long_name] => United States
[short_name] => US
[types] => Array (
[0] => country
[1] => political
)
)
[6] => stdClass Object (
[long_name] => 91007
[short_name] => 91007
[types] => Array ( [0] => postal_code )
)
)
[formatted_address] => 180 Campus Dr, Arcadia, CA 91007, USA
[geometry] => stdClass Object (
[location] => stdClass Object (
[lat] => 34.1301857
[lng] => -118.0333596
)
[location_type] => ROOFTOP
[viewport] => stdClass Object (
[northeast] => stdClass Object (
[lat] => 34.131534680292
[lng] => -118.03201061971
)
[southwest] => stdClass Object (
[lat] => 34.128836719709
[lng] => -118.03470858029
)
)
)
[partial_match] => 1
[types] => Array ( [0] => street_address )
)
)
这存储在名为$results
编辑: 虽然这已经得到了回答,(谢谢),这就是我的尝试:
foreach($results as $key=>$value){
echo $key.' : '.$value.'<br />';
}
它抛出了一个错误:Object of class stdClass could not be converted to string
我知道我正在错误地访问它,但是我不记得我做错了什么。
答案 0 :(得分:9)
echo $results[0]->address_components[0]->long_name;
答案 1 :(得分:2)
foreach( $results[0]->address_components as $id => $address )
{
echo "\n$id. address: {$address->long_name}\n";
foreach ( $address->types as $type )
{
echo $type , ' ';
}
}
echo $results[0]->geometry->viewport->southwest->lat;
并且,如果它是从json_decode()
获得的,那么您应该注意该函数的第二个参数。如果true
,它将创建您的旧数组。