0

我的 xml-php-script 有问题。XML 文档:http ://www.yr.no/place/Sweden/Stockholm/Stockholm/forecast.xml

$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

4

2 回答 2

0

如果你得到输出

echo $temp->温度,'',$temp->windSpeed,PHP_EOL;

从此,您可以获得如下温度值

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;
    }
于 2013-06-19T11:20:56.410 回答
0

有多个<time>s,所以你可以循环它们。此外,temperatureandwindSpeed在属性中。

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;
于 2013-06-19T11:23:43.220 回答