Psycho Posted October 18, 2007 Share Posted October 18, 2007 I am converting an XML file into an object via $xml_data = simplexml_load_file('xmldata\test.xml'); However I run into a problem if I try to use a value within that XML object as a string: <?php $id = $xml_data->id; $array[$id]['keyname'] = $xml_data->keyname; ?> Results in the error Warning: "Warning Illegal offset type in C:\xampp\htdocs_dvd\xml.php on line 120" I can avoid the error by enclosing the value in quotes when assigning to a variable such as this: <?php $id = "$xml_data->id"; $array[$id]['keyname'] = $xml_data->keyname; ?> Is this the best approach? Quote Link to comment https://forums.phpfreaks.com/topic/73840-solved-converting-xml-object-to-string/ Share on other sites More sharing options...
Barand Posted October 18, 2007 Share Posted October 18, 2007 or you can cast it as string $id = (string)$xml_data->id; Quote Link to comment https://forums.phpfreaks.com/topic/73840-solved-converting-xml-object-to-string/#findComment-372532 Share on other sites More sharing options...
Psycho Posted October 18, 2007 Author Share Posted October 18, 2007 Ok, thanks. That will help whenever I have to go back and edit the code as it will be more apparent what is happening. If I was to use just double quotes I might see that later and think I don't need them. Quote Link to comment https://forums.phpfreaks.com/topic/73840-solved-converting-xml-object-to-string/#findComment-372633 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.