[英]SimpleXML storing attributes in an array
My code is sort of working but I'm not getting the output which I want, I literally just want the output "Gentle Breeze" but instead I am getting "SimpleXMLElement Object ( [0] => Gentle Breeze )" 我的代码可以正常工作,但是我没有得到想要的输出,我只是想要输出“ Gentle Breeze”,而是得到“ SimpleXMLElement Object([0] => Gentle Breeze)”
Here is my code: 这是我的代码:
<head>
<title>Open Weather API</title>
</head>
<body>
<?php
$url = "http://api.openweathermap.org/data/2.5/forecast/daily?q=London&mode=xml&units=metric&cnt=16&appid=Key";
$xml = simplexml_load_file($url);
foreach($xml->forecast->time as $times)
{
$windspeed[] = $times->windSpeed["name"];
}
print_r($windspeed[0])
?>
</body>
I have tried using echo instead of print_r but all that gets outputted is "Array" 我尝试使用echo代替print_r,但是输出的全部是“ Array”
Here is an example of the XML I am pulling from the API: 这是我从API中提取的XML的示例:
<time day="2016-08-19">
<symbol number="500" name="light rain" var="10d"/>
<precipitation value="2.97" type="rain"/>
<windDirection deg="188" code="S" name="South"/>
<windSpeed mps="5.28" name="Gentle Breeze"/>
<temperature day="22.58" min="17.06" max="22.58" night="18.39" eve="18.61" morn="17.06"/>
<pressure unit="hPa" value="1012.32"/>
<humidity value="72" unit="%"/>
<clouds value="overcast clouds" all="92" unit="%"/>
</time>
I need to store it in an array so I can call it later on in my code, I'm just outputting the results as a test to see what is stored in the array and I just want it to store "Gentle Breeze" not "SimpleXMLElement Object ( [0] => Gentle Breeze )" 我需要将其存储在数组中,以便稍后在代码中调用它,我只是将结果作为测试输出,以查看存储在数组中的内容,而我只希望它存储“ Gentle Breeze”而不是“ SimpleXMLElement对象([0] =>温和的微风”“
A simple cast to string should achieve what you want: 一个简单的字符串转换应该可以实现您想要的:
foreach($xml->forecast->time as $times)
{
$windspeed[] = (string) $times->windSpeed["name"];
}
try doing this instead: 尝试这样做:
$windspeed[] = $times->windSpeed->name;
or 要么
var_dump ($times->windSpeed->name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.