PHP arrayToXml() function

Table of Contents

PHP arrayToXml() function

/**
 * Convert an array to XML
 * @param array $array
 * @param SimpleXMLElement $xml
 */
function arrayToXml($array, &$xml){
    foreach ($array as $key => $value) {
        if(is_array($value)){
            if(is_int($key)){
                $key = "e";
            }
            $label = $xml->addChild($key);
            $this->arrayToXml($value, $label);
        }
        else {
            $xml->addChild($key, $value);
        }
    }
}

// $xml = new SimpleXMLElement('<root/>');
// array_walk_recursive($arr1, array ($xml, 'addChild'));
// print $xml->asXML();

Leave a Reply

Your email address will not be published. Required fields are marked *