2011-04-03 76 views
1

我不unserstand为什么我得到这样的错误:非法使用偏移动态变量

警告: 非法偏移类型.../index.php文件上线10美元1.4141

警告:非法上线 偏移类型.../index.php的10日元118.56

这里是我的代码:

<?php 
$xml = simplexml_load_file("eurofxref-daily.xml"); 
$array=array(); 
foreach ($xml->children() as $cubeMain) { 
    foreach ($cubeMain->children() as $cubeTime) { 
     echo "Time: " . $cubeTime['time']; 
     foreach ($cubeTime->children() as $cubeCurr) { 
      $currency=$cubeCurr['currency']; 
      $rate=$cubeCurr['rate']; 
      $array = array($currency => $rate); 
      echo $currency . "&nbsp;" . $rate . "<br />"; 
     } 
    } 
} 
?> 

回答

5

使用$array = array((string)$currency => $rate);

SimpleXML返回对象,而不是字符串 - 虽然它们有适当的__toString方法,但是当这些对象用作数组索引时,PHP不会使用它们。

+0

Ty,但是如何在后面回显键值? – Marko 2011-04-03 08:42:14

+0

你是什么意思?使用对象本身'回显'它们是很好的,因为它们在字符串上下文中使用时正确转换为字符串。你的代码甚至可以做到这一点...... – ThiefMaster 2011-04-03 08:43:12

+0

我的目标是:1)将所有货币汇率转换为数组2)从数组中打印货币和汇率。 – Marko 2011-04-03 08:45:23