我收到一个邮政编码API的回复.但是,由于两个词之间存在空格,我无法弄清楚“地名”中的值.不太确定从这里去哪里.
object(stdClass)#1 (4) {
["post code"]=>
string(5) "42223"
["country"]=>
string(13) "United States"
["country abbreviation"]=>
string(2) "US"
["places"]=>
array(1) {
[0]=>
object(stdClass)#2 (5) {
["place name"]=>
string(13) "Fort Campbell"
["longitude"]=>
string(8) "-87.5585"
["state"]=>
string(8) "Kentucky"
["state abbreviation"]=>
string(2) "KY"
["latitude"]=>
string(7) "36.5995"
}
}
}
解决方法:
您需要将它们放在大括号内并用单引号引起来:
$place_name = $response->places[0]->{'place name'};
echo $place_name;
或如@scragar在评论中所述,如果您不方便通过对象访问它们,则可以在json_decode($response,true)上放置true标志,以便可以将它们作为关联数组访问.
标签:php,arrays,json,object
来源: https://codeday.me/bug/20191011/1892105.html