从 SimpleXMLElement 对象获取值
我有这样的事情:
$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
执行,它返回一个对象。我怎样才能得到这个值?
I have something like this:
$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
);
It's supposed to download XML from geonames. If I do print_r($xml)
I get :
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
)
I do as you can see $xml->code[0]->lat
and it returns an object. How can i get the value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您必须将 simpleXML 对象转换为字符串。
You have to cast simpleXML Object to a string.
您还可以使用魔术方法 __toString()
You can also use the magic method __toString()
如果您知道 XML 元素的值是浮点数(纬度、经度、距离),则可以使用
(float)
另外,
(int)
表示整数:If you know that the value of the XML element is a float number (latitude, longitude, distance), you can use
(float)
Also,
(int)
for integer number:如果您不知道XML元素的值,您可以使用
它在您需要确定值是否是数字时起作用,因为
(string)
将始终返回字符串并且is_int($value)
返回false
if you don't know the value of XML Element, you can use
It works when you need to determine if value is a number, because
(string)
will always return string andis_int($value)
returnsfalse
如果您赶时间,可以快速解决。
将 Xml 对象转换为数组(或对象),
Quick Solution if you in hurry.
convert a Xml-Object to an array (or object),
您可以使用“{}”来访问您的属性,然后您就可以按照您的意愿进行操作。
保存或显示内容。
从你的例子来看,她的代码
you can use the '{}' to access you property, and then you can do as you wish.
Save it or display the content.
From your example her's the code
这个函数一直帮助我将 xml 相关值转换为数组
This is the function that has always helped me convert the xml related values to array
尝试
current($xml->code[0]->lat)
它返回数组当前指针下的元素,即 0,因此您将获得值
try
current($xml->code[0]->lat)
it returns element under current pointer of array, which is 0, so you will get value
您可以使用此函数转换数组
you can convert array with this function