How to read SimpleXML Object in PHP


 
How can I properly parse a SimpleXML Object in PHP?

It is really simple to access attributes using array form. However, you must convert them to strings or ints if you plan on passing the values to functions.

You have to cast simpleXML Object to a string.

$value = (string) $xml->code[0]->lat;

If you know that the value of the XML element is a float number (latitude, longitude, distance), you can use (float)

$value = (float) $xml->code[0]->lat;

Also, (int) for integer number:

$value = (int) $xml->code[0]->distance;

You can more read about simplexmlelement.

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

Advertisements