2013-06-19 76 views
0

我的xml-php-script有問題。 XML文檔:http://www.yr.no/place/Sweden/Stockholm/Stockholm/forecast.xmlXML到PHP變量

$xmlstr = "http://www.yr.no/place/Sweden/Stockholm/Stockholm/forecast.xml"; 

//$vader = new SimpleXMLElement($xmlstr); 
$vader = simplexml_load_file($xmlstr); 

foreach ($vader->forecast->tabular->time->temperature as $temp) { 
    echo $temp->temperature, ' ', $temp->windSpeed, PHP_EOL; 
} 

我想在一個變溫名爲:$temp

+0

您好,感謝 我補充weatherdata,但還是不行。 url:http://soldagar.se/ajja.php – Andreas

+0

不,你不應該添加'weatherdata',因爲那是根節點。看到我的答案。 – MrCode

回答

0

有多種<time> S,這樣你就可以循環的。此外,temperaturewindSpeed屬於屬性。

foreach ($vader->forecast->tabular->time as $time) { 
    echo $time->temperature->attributes()->value, ' ', $time->windSpeed->attributes()->mps, PHP_EOL; 

    // or store temperature in a variable? 
    // $temp = $time->temperature; 
    // echo $temp->attributes()->value; // 14 
    // echo $temp->attributes()->unit; // celcius 
} 

或者訪問僅僅是第一<time>

echo $vader->forecast->tabular->time[0]->temperature->attributes()->value; 
echo $vader->forecast->tabular->time[0]->windSpeed->attributes()->mps; 
0

如果您收到輸出

回聲$ TEMP->溫度, '',$ TEMP->風速, PHP_EOL;

從這個那麼你可以得到tenperature值如下

PHP代碼:

$temp_array = array(); 
    $tempt_str = '' 
    $i = 0; 
    foreach ($vader->forecast->tabular->time->temperature as $temp) { 
     $tempt_str .= 'temperature : '.$temp->temperature.''.'windSpeed : '.$temp->windSpeed.'<br/>'; 
     $temp_array[$i]['temperature'] = $temp->temperature; 
     $temp_array[$i]['windSpeed'] = $temp->windSpeed; 
    }