xml2array() - XML Parser for PHP
xml2array() is a easy to use PHP function that will convert the given XML text to an array in the XML structure. Kind of like my Javascript xml2array() function.
For example, this XML...
<?xml version="1.0" encoding="iso-8859-1"?>
<languages>
<lang type='interpreted'>
<name know='true' application='web'>PHP</name>
<name know='true'>Python</name>
</lang>
<lang type='compiled'>
<name know='true'>C++</name>
<name>Java</name>
</lang>
</languages>
Will be converted to...
Array
(
[languages] => Array
(
[lang] => Array
(
[0] => Array
(
[attr] => Array
(
[type] => interpreted
)
[name] => Array
(
[0] => Array
(
[value] => PHP
[attr] => Array
(
[know] => true
[application] => web
)
)
[1] => Array
(
[value] => Python
[attr] => Array
(
[know] => true
)
)
)
)
[1] => Array
(
[attr] => Array
(
[type] => compiled
)
[name] => Array
(
[0] => Array
(
[value] => C++
[attr] => Array
(
[know] => true
)
[1] => Array
(
[value] => Java
)
)
)
)
)
)
Using the simple code...
$contents = file_get_contents('sample.xml');//Or however you what it
$result = xml2array($contents);
print_r($result);
This is kind of verbose - this is because I had to support the attributes in XML. There is an option in this function to ignore all attributes. The same XML produces this output.
Array
(
[languages] => Array
(
[lang] => Array
(
[0] => Array
(
[name] => Array
(
[0] => PHP
[1] => Python
)
)
[1] => Array
(
[name] => Array
(
[0] => C++
[1] => Java
)
)
)
)
)
This can be done using a second argument to the function...
$contents = file_get_contents('sample.xml');//Or however you what it
$result = xml2array($contents,0);
print_r($result);

Comments
i think that the place of putting the above code was wrong.
can u tell me where to put the above code.
Good Job,
I tried using it for my application , it dint work.
I have the xml file www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml and i have to extract each begining of the month the values for an certain currency ex (USD and RON) for the month that passed and to put them into an database . ex on 01.01.2010 to extract the date, the values of usd and RON for the period 01.12.2009 - 31.12.2009.
until now as i told you it`s the only script that is working properly.
I`m an beginer in this and all the help will be appreciated .
Thank you in advance.
a, strong, em, b, i, code, pre, pandbrallowed. Other tags will be shown as code(< will become <). Urls, Line breaks will be auto-formated.