1

我正在尝试创建一个 PHP 查询,该查询可以使用查询中的动态变量访问 XML 提要。

$xml = new SimpleXMLElement($row['feed_result']); 
$a = 'Sensor';
$b = 'Condition';
$c = 'LastReading';
echo 'Sensor 1 : '.$xml->{$a}[0]->{$b}[6]->{$c}.'<br />';

这对我有用,即我得到了正确的值作为传感器 1 的响应。但是,我完全无法尝试使 [0] 和 [6] 成为动态查询的一部分。例如,如果我像这样更改代码,查询失败并且没有响应,

$xml = new SimpleXMLElement($row['feed_result']); 
$a = 'Sensor[0]';
$b = 'Condition[6]';
$c = 'LastReading';
echo 'Sensor 1 : '.$xml->{$a}->{$b}->{$c}.'<br />';

我的问题是如何使方括号内的部分成为动态查询的一部分?当方括号是变量的一部分时,为什么查询不起作用?

XML 如下所示:

<CurrentConditions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="" Status="0" Latitude="0.0" Longitude="0.0" Units="Metric">
<Sensor Name="sensor01" Port="1" Status="0">
<Condition Type="Humidity">
<DateReceived>2013-06-30T17:42:59.237Z</DateReceived>
<LastReading>56.00</LastReading>
<LastReadingDisplay>56.00 %</LastReadingDisplay>
</Condition>
<Condition Type="Humidity">
<DateReceived>2013-06-30T17:45:20.66Z</DateReceived>
<LastReading>57.00</LastReading>
<LastReadingDisplay>57.00 %</LastReadingDisplay>
</Condition>
<Condition Type="Temperature">
<DateReceived>2013-07-27T08:45:38.593Z</DateReceived>
<LastReading>25.20</LastReading>
<LastReadingDisplay>25.20 C</LastReadingDisplay>
</Condition>
<Condition Type="Temperature">
<DateReceived>2013-07-27T08:50:51.36Z</DateReceived>
<LastReading>25.30</LastReading>
<LastReadingDisplay>25.30 C</LastReadingDisplay>
</Condition>
<Condition Type="Temperature">
<DateReceived>2013-07-27T08:56:02.283Z</DateReceived>
<LastReading>25.30</LastReading>
<LastReadingDisplay>25.30 C</LastReadingDisplay>
</Condition>
<Condition Type="Temperature">
<DateReceived>2013-07-27T09:01:14.313Z</DateReceived>
<LastReading>25.30</LastReading>
<LastReadingDisplay>25.30 C</LastReadingDisplay>
</Condition>
<Condition Type="Temperature">
<DateReceived>2013-07-27T09:06:26.797Z</DateReceived>
<LastReading>25.30</LastReading>
<LastReadingDisplay>25.30 C</LastReadingDisplay>
</Condition>
</Sensor>
<Sensor Name="sensor02" Port="2" Status="0">
<Condition Type="Humidity">
<DateReceived>2013-07-27T08:45:38.593Z</DateReceived>
<LastReading>17.00</LastReading>
<LastReadingDisplay>17.00 %</LastReadingDisplay>
</Condition>
<Condition Type="Temperature">
<DateReceived>2013-07-27T08:45:38.593Z</DateReceived>
<LastReading>44.10</LastReading>
<LastReadingDisplay>44.10 C</LastReadingDisplay>
</Condition>
<Condition Type="Humidity">
<DateReceived>2013-07-27T08:50:51.36Z</DateReceived>
<LastReading>18.00</LastReading>
<LastReadingDisplay>18.00 %</LastReadingDisplay>
</Condition>
<Condition Type="Temperature">
<DateReceived>2013-07-27T08:50:51.36Z</DateReceived>
<LastReading>44.40</LastReading>
<LastReadingDisplay>44.40 C</LastReadingDisplay>
</Condition>
<Condition Type="Humidity">
<DateReceived>2013-07-27T08:56:02.283Z</DateReceived>
<LastReading>18.00</LastReading>
<LastReadingDisplay>18.00 %</LastReadingDisplay>
</Condition>
<Condition Type="Temperature">
<DateReceived>2013-07-27T08:56:02.283Z</DateReceived>
<LastReading>44.90</LastReading>
<LastReadingDisplay>44.90 C</LastReadingDisplay>
</Condition>
<Condition Type="Humidity">
<DateReceived>2013-07-27T09:01:14.313Z</DateReceived>
<LastReading>17.00</LastReading>
<LastReadingDisplay>17.00 %</LastReadingDisplay>
</Condition>
<Condition Type="Temperature">
<DateReceived>2013-07-27T09:01:14.313Z</DateReceived>
<LastReading>44.40</LastReading>
<LastReadingDisplay>44.40 C</LastReadingDisplay>
</Condition>
<Condition Type="Humidity">
<DateReceived>2013-07-27T09:06:26.797Z</DateReceived>
<LastReading>16.00</LastReading>
<LastReadingDisplay>16.00 %</LastReadingDisplay>
</Condition>
<Condition Type="Temperature">
<DateReceived>2013-07-27T09:06:26.797Z</DateReceived>
<LastReading>45.80</LastReading>
<LastReadingDisplay>45.80 C</LastReadingDisplay>
</Condition>
</Sensor>
</CurrentConditions>

谢谢

4

1 回答 1

1

您是否正在寻找这样的数组索引变量?

<?php

$xml = new SimpleXMLElement($row['feed_result']); 
$a = 'Sensor';
$aindex = 0; // <------
$b = 'Condition';
$bindex = 6; // <------
$c = 'LastReading';
echo 'Sensor 1 : '.$xml->{$a}[$aindex]->{$b}[$bindex]->{$c}.'<br />';

?>
于 2013-07-27T09:29:39.767 回答