我正在编写一个 PHP 脚本,其中包含以下for
循环:
for($x=count($content->data->parameters[0]->weather->children());$content->data->weather->{"weather-conditions"}[$x]['weather-type']==NULL;--$x){
echo $x."\n";
}
其中 $content 是 SimpleXMLElement 的一个实例,它解析 XML 如下:
<dwml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:noNamespaceSchemaLocation="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">
<data>
<parameters applicable-location="point1">
<location>
<weather time-layout="k-p3h-n41-3">
<name>Weather Type, Coverage, and Intensity</name>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="likely" intensity="light" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="definitely" intensity="light" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="definitely" intensity="light" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="definitely" intensity="moderate" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="areas" intensity="none" additive="and" weather-type="blowing snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="definitely" intensity="moderate" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="areas" intensity="none" additive="and" weather-type="blowing snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="definitely" intensity="moderate" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="areas" intensity="none" additive="and" weather-type="blowing snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="definitely" intensity="moderate" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="areas" intensity="none" additive="and" weather-type="blowing snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="areas" intensity="none" additive="and" weather-type="blowing snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="areas" intensity="none" additive="and" weather-type="blowing snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="snow showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="snow showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="snow showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="snow showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="slight chance" intensity="light" additive="and" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="slight chance" intensity="light" additive="and" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="chance" intensity="light" additive="and" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="chance" intensity="light" additive="and" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="chance" intensity="light" additive="and" weather-type="snow" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
</weather>
</parameters>
</data>
</dwml>
编辑:解析此文件后,这就是var_dump($content->data->parameters[0]->weather);
产生的结果:
object(SimpleXMLElement)#4 (3) {
["@attributes"]=>
array(1) {
["time-layout"]=>
string(11) "k-p3h-n41-3"
}
["name"]=>
string(37) "Weather Type, Coverage, and Intensity"
["weather-conditions"]=>
array(41) {
[0]=>
object(SimpleXMLElement)#2 (1) {
["value"]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(13) "slight chance"
["intensity"]=>
string(5) "light"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#46 (0) {
}
}
}
[1]=>
object(SimpleXMLElement)#5 (1) {
["value"]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(13) "slight chance"
["intensity"]=>
string(5) "light"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#46 (0) {
}
}
}
[2]=>
object(SimpleXMLElement)#6 (1) {
["value"]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(6) "chance"
["intensity"]=>
string(5) "light"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#46 (0) {
}
}
}
[3]=>
object(SimpleXMLElement)#7 (1) {
["value"]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(6) "likely"
["intensity"]=>
string(5) "light"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#46 (0) {
}
}
}
[4]=>
object(SimpleXMLElement)#8 (1) {
["value"]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(10) "definitely"
["intensity"]=>
string(5) "light"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#46 (0) {
}
}
}
[5]=>
object(SimpleXMLElement)#9 (1) {
["value"]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(10) "definitely"
["intensity"]=>
string(5) "light"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#46 (0) {
}
}
}
[6]=>
object(SimpleXMLElement)#10 (1) {
["value"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(10) "definitely"
["intensity"]=>
string(8) "moderate"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
[1]=>
object(SimpleXMLElement)#46 (2) {
["@attributes"]=>
array(5) {
["coverage"]=>
string(5) "areas"
["intensity"]=>
string(4) "none"
["additive"]=>
string(3) "and"
["weather-type"]=>
string(12) "blowing snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
}
}
[7]=>
object(SimpleXMLElement)#11 (1) {
["value"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#46 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(10) "definitely"
["intensity"]=>
string(8) "moderate"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
[1]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(5) {
["coverage"]=>
string(5) "areas"
["intensity"]=>
string(4) "none"
["additive"]=>
string(3) "and"
["weather-type"]=>
string(12) "blowing snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
}
}
[8]=>
object(SimpleXMLElement)#12 (1) {
["value"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(10) "definitely"
["intensity"]=>
string(8) "moderate"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
[1]=>
object(SimpleXMLElement)#46 (2) {
["@attributes"]=>
array(5) {
["coverage"]=>
string(5) "areas"
["intensity"]=>
string(4) "none"
["additive"]=>
string(3) "and"
["weather-type"]=>
string(12) "blowing snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
}
}
[9]=>
object(SimpleXMLElement)#13 (1) {
["value"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#46 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(10) "definitely"
["intensity"]=>
string(8) "moderate"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
[1]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(5) {
["coverage"]=>
string(5) "areas"
["intensity"]=>
string(4) "none"
["additive"]=>
string(3) "and"
["weather-type"]=>
string(12) "blowing snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
}
}
[10]=>
object(SimpleXMLElement)#14 (1) {
["value"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(6) "chance"
["intensity"]=>
string(5) "light"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
[1]=>
object(SimpleXMLElement)#46 (2) {
["@attributes"]=>
array(5) {
["coverage"]=>
string(5) "areas"
["intensity"]=>
string(4) "none"
["additive"]=>
string(3) "and"
["weather-type"]=>
string(12) "blowing snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
}
}
...//More in here, trivial.
[40]=>
object(SimpleXMLElement)#44 (1) {
["value"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#45 (2) {
["@attributes"]=>
array(4) {
["coverage"]=>
string(6) "chance"
["intensity"]=>
string(5) "light"
["weather-type"]=>
string(4) "rain"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
[1]=>
object(SimpleXMLElement)#46 (2) {
["@attributes"]=>
array(5) {
["coverage"]=>
string(6) "chance"
["intensity"]=>
string(5) "light"
["additive"]=>
string(3) "and"
["weather-type"]=>
string(4) "snow"
["qualifier"]=>
string(4) "none"
}
["visibility"]=>
object(SimpleXMLElement)#47 (0) {
}
}
}
}
}
}
由于最后的元素有时会是空的,我想要最新的非空天气项目,我将 x 设置为天气数组中最后一个项目的索引,并尝试向后工作,但页面没有加载,并且错误日志中没有任何内容。编辑:我已将方法更改为此,以使其更易于理解:
for($x=0; $content->data->parameters[0]->weather->{"weather-conditions"}[count($content->data->parameters[0]->weather->children())-$x]['weather-type']==NULL; $x++){
echo $x."\n";
}
编辑:如果不清楚,我需要通过子数组向后递归,而不是向前。