【发布时间】:2011-02-21 11:50:40
【问题描述】:
我有这样的事情:
$url = "http://ws.geonames.org/findNearbyPostalCodes?country=pl&placename=";
$url .= rawurlencode($city[$i]);
$xml = simplexml_load_file($url);
echo $url."\n";
$cityCode[] = array(
'city' => $city[$i],
'lat' => $xml->code[0]->lat,
'lng' => $xml->code[0]->lng
);
它应该从 geonames 下载 XML。如果我这样做print_r($xml) 我会得到:
SimpleXMLElement Object
(
[code] => Array
(
[0] => SimpleXMLElement Object
(
[postalcode] => 01-935
[name] => Warszawa
[countryCode] => PL
[lat] => 52.25
[lng] => 21.0
[adminCode1] => SimpleXMLElement Object
(
)
[adminName1] => Mazowieckie
[adminCode2] => SimpleXMLElement Object
(
)
[adminName2] => Warszawa
[adminCode3] => SimpleXMLElement Object
(
)
[adminName3] => SimpleXMLElement Object
(
)
[distance] => 0.0
)
如你所见$xml->code[0]->lat,它返回一个对象。我怎样才能获得价值?
【问题讨论】:
-
2017 更新:SO 不再在顶部显示最佳答案。 The best answer is here.
-
@rinogo 您可能不小心单击了答案块顶部的排序选项卡之一。您链接到的答案有 345 票,因此如果您将排序设置为“投票”,则显示在顶部。
-
谢谢,@IMSoP!你是对的 - 我一定在某个时候点击了“活动”(对于旧问题的答案过时有用,顺便说一句) - 很高兴知道我需要将它改回“投票”! :)