Development #php#xml 1 min

php: SimpleXML and xml2array

Translation of the original in Catalan. Read it in: CatalàCastellano

Two very powerful PHP functions, especially now that every five minutes we have to work with XML files.

$xml = simplexml_load_file('fitxer.xml');

It reads fitxer.xml from the hard drive and turns it into an object of the SimpleXMLElement class. To see what it turns into you can use print_r.

Now that we have the data as an object, if we still don't like it we can turn it into an array with a single function:

$varArray = get_object_vars($objXml);

Also with a print_r we can see an array of the usual kind.

Often the power is in the simplicity... it's worth keeping these functions in mind.