1
$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
  • 2
    Do a var_dump($dag). You'll probably find that it's actually an object, which can't be used as an array index. Commented Sep 30, 2011 at 17:53

1 Answer 1

4

$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;
Sign up to request clarification or add additional context in comments.

3 Comments

Pancakes with loads of sugar for you my friend. Thanks!
Yay! I'll bring the lemon juice.
oh php; sometimes things you do elude me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.