Page 1 of 1
Convert SimpleXMLElement
Posted: Fri Dec 18, 2009 10:27 am
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;
}
Re: Convert SimpleXMLElement
Posted: Fri Dec 18, 2009 10:38 am
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;
}
Re: Convert SimpleXMLElement
Posted: Fri Dec 18, 2009 11:24 am
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.
Re: Convert SimpleXMLElement
Posted: Fri Dec 18, 2009 12:00 pm
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;
}
Re: Convert SimpleXMLElement
Posted: Fri Dec 18, 2009 12:09 pm
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?
Re: Convert SimpleXMLElement
Posted: Fri Dec 18, 2009 12:53 pm
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.
Re: Convert SimpleXMLElement
Posted: Fri Dec 18, 2009 3:44 pm
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