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);
Subscribe to Feed