我有以下代码从XML test.xml获取Channel和AP Count值。我需要根据这些结果创建一个PHP Jpgraph。因此,我需要将 $ channel 和 $ apcount 的for循环结果存储到两个单独的数组中:x-axis []和y-axis []
<?
$feed = file_get_contents("test.xml");
$xml = new SimpleXmlElement($feed);
$xml->registerXPathNamespace("ns", "urn:broadband-forum-org:ipdr:tr-232-1-0");
$number_of_entries = $xml->xpath("//ns:BulkData[ns:Name='InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.ResultNumberOfEntries']/ns:Value");
$number_of_entries = $number_of_entries[0];
for ($i = 1; $i <= $number_of_entries; $i++) {
$channel = $xml->xpath("//ns:BulkData[ns:Name='InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.{$i}.Channel']/ns:Value");
$channel = $channel[0];
$apcount = $xml->xpath("//ns:BulkData[ns:Name='InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.{$i}.APcount']/ns:Value");
$apcount = $apcount[0];
}
?>
这是test.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<ipdr:IPDRDoc xmlns:ipdr="http://www.ipdr.org/namespaces/ipdr"
xmlns="urn:broadband-forum-org:ipdr:tr-232-1-0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:broadband-forum-org:ipdr:tr-232-1-0 tr-232-1-0-0-serviceSpec.xsd http://www.ipdr.org/namespaces/ipdr http://www.ipdr.org/public/IPDRDoc3.5.1.xsd"
docId="74697373-6f74-7878-7878-746973736f74"
creationTime="2013-06-11T06:02:55.153Z"
IPDRRecorderInfo="IPDR Collector" version="3.5.1">
<ipdr:IPDR xsi:type="BulkDataReport">
<BulkData>
<Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.ResultNumberOfEntries</Name>
<Value>3</Value>
</BulkData>
<BulkData>
<Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.1.Channel</Name>
<Value>1</Value>
</BulkData>
<BulkData>
<Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.1.APcount</Name>
<Value>3</Value>
</BulkData>
<BulkData>
<Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.2.Channel</Name>
<Value>6</Value>
</BulkData>
<BulkData>
<Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.2.APcount</Name>
<Value>8</Value>
</BulkData>
<BulkData>
<Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.3.Channel</Name>
<Value>11</Value>
</BulkData>
<BulkData>
<Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.3.APcount</Name>
<Value>6</Value>
</BulkData>
</ipdr:IPDR >
<ipdr:IPDRDoc.End count="1" endTime="2013-06-11T06:02:55.207Z"></ipdr:IPDRDoc.End>
</ipdr:IPDRDoc>
我期待的最终结果是两个数组:
print_r($x-axis);
Array (
[0] => 1
[1] => 6
[2] => 11
)
print_r($y-axis);
Array (
[0] => 3
[1] => 8
[2] => 6
)
答案 0 :(得分:0)
使用这样的代码
在循环初始化两个数组之前
$y-axis=array();
$x-axis=array();
内循环
$x-axis[]=$channel[0];
$y-axis[]=$apcount[0];
答案 1 :(得分:0)
@rakimo你的建议让我走了一半路。我在循环之后添加了下面的代码,它运行得很好。谢谢!
foreach ($x-axis as $k => &$v) {
$a = (array)$v;
$v = $a[0];
}
foreach ($y-axis as $s => &$t) {
$b = (array)$t;
$t = $b[0];
}