我不知道为什么我会得到这样的错误:
警告:非法偏移类型在.../index.php第10行USD 1.4141
警告:第10行JPY 118.56中的.../index.php中的非法偏移类型
这是我的代码:
<?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 . " " . $rate . "<br />";
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
使用$array = array((string)$currency => $rate);.
SimpleXML返回对象而不是字符串 - 虽然它们有适当的__toString方法,但当这些对象用作数组索引时,PHP不会使用它们.