create xml to array by php 2 comments

function xml2array($xml) {
    $xmlArray = array();
    $regexp = "/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s";
    preg_match_all($regexp, $xml, $elements);
    foreach ($elements[1] as $ie => $element) {
        if (preg_match($regexp, $elements[3][$ie]))
            $xmlArray[$element] = xml2array($elements[3][$ie]);
        else if ($elements[3][$ie]) {
            $xmlArray[$element] = trim($elements[3][$ie]);
    return $xmlArray;
$xml=file_get_contents('pragnesh.xml');//2011-07-26_12-27-04.xml, ebay.xml, 002-8836553-5280256.xml
$xml = xml2array($xml);
echo '<pre>';
echo '</pre>';

this is ‘pragnesh.xml’

    <title>C programming</title>
    <author rating="3">Balaguruswamy</author>
    <author rating="5">Vineet</author>

Share on Facebook

About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools

2 thoughts on “create xml to array by php