Convert SimpleXMLElement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Convert SimpleXMLElement

Post by JNettles »

I have an object that is created through SimpleXML - looks something like this.

Code: Select all

object(SimpleXMLElement)[36]
  public 'COURSE_OFFERING_ID' => string '83742869' (length=8)
  public 'OFFERING_NAME' => string 'Violencia causas y efectos' (length=26)
 
Because its parsed out form XML, everything is a string, so I wrote a loop that goes through each value, check to see if it can be numeric (ie: numbers only) and tries to convert it from a string type to an integer. Theoretically. I'm sure that I'm missing something small here.

I foreach through the object, looking at each value and checking to see if its numeric. If it is, I do $value = (int) $value. Should work, but it doesn't. Looking through my debugger, it looks like $value is actually a SimpleXMLElement type and I'm actually at a bit of a loss here on how to convert this thing to an integer. I can echo $value just fine but it resists being converted no matter what I do. Any thoughts?

Code: Select all

foreach($object as $row => $value)
{
    $value = (int) $value; 
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Convert SimpleXMLElement

Post by AbraCadaver »

You can't change an array/object in a foreach because it creates a copy of the vars. To change them you need to reference them:

Code: Select all

foreach($object as &$value)
{
    $value = (int) $value;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Convert SimpleXMLElement

Post by JNettles »

...... that doesn't make sense. Key elements and iterators in a foreach loop can't be passed by reference - it'll kick a parser error at you.

I think I have an idea - it turns out my object is actually a SimpleXMLElement as well; do SimpleXMLElement objects automatically force their values as strings? It would make sense, since an XML document would treat it as a string.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Convert SimpleXMLElement

Post by AbraCadaver »

JNettles wrote:...... that doesn't make sense. Key elements and iterators in a foreach loop can't be passed by reference - it'll kick a parser error at you.

I think I have an idea - it turns out my object is actually a SimpleXMLElement as well; do SimpleXMLElement objects automatically force their values as strings? It would make sense, since an XML document would treat it as a string.
It makes perfect sense and is the way it works since PHP 5 (did you try it?). If you are using PHP 4, then you will have to use another kind of loop and change the actual value or this should work as well (however my first reply is the standard way to do this):

Code: Select all

foreach($object as $key => $value) {
    $object->$key = (int) $value;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Convert SimpleXMLElement

Post by JNettles »

Already been doing that. There's something here preventing me from converting to an integer type and I think its the fact that the class is a SimpleXMLElement and forces string types. Anyone experienced this?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Convert SimpleXMLElement

Post by AbraCadaver »

Ahh, I see that since the simplexmlelement implements iterator, then the reference doesn't work (since 5.2). It works for all other objects I have tried.

I would assume that since we are both seeing the properties as a strings even after the cast, that, yes it is forcing a string type. You can either cast it when you need to use it instead of having it as int in the object, or if you control the XML there are data type specs for XML, however I don't know if simplexml respects these.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Convert SimpleXMLElement

Post by JNettles »

Screw it. In the amount of time I've spent trying to figure out how to do it with SimpleXML I could have written my own parser three times over by now. Thanks anyway.

Ha, the one time PHP forces a complex type it gets in the way! lol
Post Reply