1

クエリ内で動的変数を使用して XML フィードにアクセスできる PHP クエリを作成しようとしています。

$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 に答える