PHP XML:非法偏移,但它在数组中?
$dagen = array(
'Mon' => 'Maandag',
'Tue' => 'Dinsdag',
'Wed' => 'Woensdag',
'Thu' => 'Donderdag',
'Fri' => 'Vrijdag',
'Sat' => 'Zaterdag',
'Sun' => 'Zondag'
);
foreach ($xml->verwachtingen->verwachting as $verwachting) {
$graden = $verwachting->maxtempGRC - $verwachting->mintempGRC;
$graden = $graden / 2;
$graden = $graden + $verwachting->mintempGRC;
$dag = $verwachting->dagvdweek;
echo 'Op '. $dagen[$dag] .' wordt het '. $graden .' graden';
}
$xml 是使用 SimpleXMLElement 加载的 XML 文档。
现在,帮我一下吧。当我回显 $dag
时,它显示“星期五”,因为今天是星期五。所以我尝试将当时的英语单词转换为我的语言(荷兰语)。但它似乎不起作用,因为我得到这个:
Warning: Illegal offset type in C:\data\home\www\awnl-xml\index.php on line 21 Op wordt het 18.5 graden Warning: Illegal offset type in C:\data\home\www\awnl-xml\index.php on line 21 Op wordt het 18 graden Warning: Illegal offset type in C:\data\home\www\awnl-xml\index.php on line 21 ...
有人知道我为什么会收到此错误吗?谢谢。
$dagen = array(
'Mon' => 'Maandag',
'Tue' => 'Dinsdag',
'Wed' => 'Woensdag',
'Thu' => 'Donderdag',
'Fri' => 'Vrijdag',
'Sat' => 'Zaterdag',
'Sun' => 'Zondag'
);
foreach ($xml->verwachtingen->verwachting as $verwachting) {
$graden = $verwachting->maxtempGRC - $verwachting->mintempGRC;
$graden = $graden / 2;
$graden = $graden + $verwachting->mintempGRC;
$dag = $verwachting->dagvdweek;
echo 'Op '. $dagen[$dag] .' wordt het '. $graden .' graden';
}
$xml is the XML document loaded using SimpleXMLElement.
Now, help me out here. When i echo $dag
it displays 'Fri' because it is Friday. So i try to convert the english words of the days to my language (dutch). But it doesn't seem to work, because i get this:
Warning: Illegal offset type in C:\data\home\www\awnl-xml\index.php on line 21 Op wordt het 18.5 graden Warning: Illegal offset type in C:\data\home\www\awnl-xml\index.php on line 21 Op wordt het 18 graden Warning: Illegal offset type in C:\data\home\www\awnl-xml\index.php on line 21 ...
Does someone know why i get this error? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$dag
将是一个SimpleXMLElement
类型的对象。不允许将对象用于数组键,这就是您收到“非法偏移类型”警告的原因。在像这样使用之前,该对象必须首先转换为合适的类型,在您的情况下它应该是一个字符串。
$dag
will be an object, of typeSimpleXMLElement
. Objects are not allowed to be used for array keys, which is why you are getting that "Illegal offset type" warning.The object must first be cast to a suitable type before being used like that, in your case it should be a string.