PHP XML:非法偏移,但它在数组中?

发布于 2024-12-08 01:22:24 字数 1042 浏览 0 评论 0原文

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-12-15 01:22:24

$dag 将是一个 SimpleXMLElement 类型的对象。不允许将对象用于数组键,这就是您收到“非法偏移类型”警告的原因。

在像这样使用之前,该对象必须首先转换为合适的类型,在您的情况下它应该是一个字符串。

$dag = (string) $verwachting->dagvdweek;

$dag will be an object, of type SimpleXMLElement. 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.

$dag = (string) $verwachting->dagvdweek;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文