PHP XML: Illegal Offset, but it is in array?

旧时模样 提交于 2019-12-11 09:57:03

问题


$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.


回答1:


$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;


来源:https://stackoverflow.com/questions/7614058/php-xml-illegal-offset-but-it-is-in-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!