Tuesday, 14 August 2018

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.

0 comments:

Post a Comment