Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:从多维数据集XML格式解析数据_Php_Xml_Parsing - Fatal编程技术网

PHP:从多维数据集XML格式解析数据

PHP:从多维数据集XML格式解析数据,php,xml,parsing,Php,Xml,Parsing,我在官方文档中找不到解决方案,因此我的方案如下: 我需要从这个xml解析数据: 这是我迄今为止实现的代码: $xml=simplexml_load_file('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'); foreach($xml->Cube->Cube as $x) { $arr[date('d-m',strtotime($x['time']))] = array(); fore

我在官方文档中找不到解决方案,因此我的方案如下:

我需要从这个xml解析数据:

这是我迄今为止实现的代码:

$xml=simplexml_load_file('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml');
foreach($xml->Cube->Cube as $x) {
    $arr[date('d-m',strtotime($x['time']))] = array();
    foreach($xml->Cube->Cube->Cube as $y) {
        $arr[(string)$y['currency']] = (float)$y['rate'];
    };
};
问题是,这段代码显然只解析第一组速率,而我需要解析每个日期的每个速率集,然后我需要用一些我不知道如何声明的东西来更改
$xml->Cube->Cube->Cube
!那可能只是个简单的问题

更新

我快到了:

foreach($xml->Cube->Cube as $x) {
    for ($i=0;$i<90;$i++) {
        foreach($xml->Cube->Cube[$i]->Cube as $y) {
                $arr[date('d-m',strtotime($x['time']))][(string)$y['currency']] = (float)$y['rate'];
        }
    }
}
foreach($xml->Cube->Cube as$x){
对于($i=0;$iCube->Cube[$i]->Cube as$y){
$arr[date('d-m',strottime($x['time'])][(字符串)$y['currency']]=(float)$y['rate'];
}
}
}

第3行出现了问题:
foreach
不接受变量
$i
,它返回为foreach()提供的
无效参数。如果我使用变量的单个索引istead(例如
0
1
),它将起作用。现在的问题是如何动态增加索引!:(

使用名称空间有一个小技巧,但代码如下:

<?php
    $xml  = simplexml_load_file('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml');
    $xml->registerXPathNamespace('d', 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref');
    $list = $xml->xpath('//d:Cube[@currency and @rate]');
?>
<!DOCTYPE html>
<html>
    <head>
        <title>xpath</title>
   </head>
   <body>
       <table>
           <tr>
               <th>id</th>
               <th>currency</th>
               <th>rate</th>
           </tr>

           <?php $count = 0; ?>
           <?php foreach ($list as $cube): ?>
           <?php $attrs = $cube->attributes(); ?>
           <tr>
               <td><?php echo ++$count; ?></td>
               <td><?php echo $attrs['currency']; ?></td>
               <td><?php echo $attrs['rate']; ?></td>
           </tr>
           <?php endforeach; ?>

        </table>
    </body>
</html>
registerXPathNamespace('d','http://www.ecb.int/vocabulary/2002-08-01/eurofxref');
$list=$xml->xpath('//d:Cube[@currency and@rate]');
?>
xpath
身份证件
通货
率

使用xslt查询怎么样?@philipp我从来没有使用过这种方法,你能举一个实际的例子吗?我在这里读过,但我不太清楚如何将它应用于这种情况
//Cube/Cube/Cube
//Cube[@currency and@rate]
返回您想要的所有
元素的列表…要在简单xml上运行查询,您应该看看这里:@philipp不认为这是我需要的。我几乎找到了解决方案,我更新了问题。我想这是
//Cube[@currency and@rate]
xpath查询将返回一个包含所有
元素的列表/数组,该列表/数组显式地包含一个currency和rate属性。您可以在该列表上进行迭代,这样就完成了。即使xml的结构发生了变化,只要它包含具有这些属性的
元素,这也会起作用。您的循环方法也会起作用,但是与编写三个嵌套循环不同,您可以只编写一个。这取决于您自己,但我认为xpath方法更健壮,可读性更强。