[英]SimpleXML storing attributes in an array
我的代碼可以正常工作,但是我沒有得到想要的輸出,我只是想要輸出“ Gentle Breeze”,而是得到“ SimpleXMLElement Object([0] => Gentle Breeze)”
這是我的代碼:
<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>
我嘗試使用echo代替print_r,但是輸出的全部是“ Array”
這是我從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>
我需要將其存儲在數組中,以便稍后在代碼中調用它,我只是將結果作為測試輸出,以查看存儲在數組中的內容,而我只希望它存儲“ Gentle Breeze”而不是“ SimpleXMLElement對象([0] =>溫和的微風”“
一個簡單的字符串轉換應該可以實現您想要的:
foreach($xml->forecast->time as $times)
{
$windspeed[] = (string) $times->windSpeed["name"];
}
嘗試這樣做:
$windspeed[] = $times->windSpeed->name;
要么
var_dump ($times->windSpeed->name);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.