2011-04-03 78 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