public static function array2XML($haystack, $rootElementName = '')
    {
        $args   = func_get_args();
        $isRoot = !isset($args[2]);
        $root   = isset($args[3]) ? $args[3] : new \DOMDocument('1.0', 'utf-8');
        $parent = isset($args[2]) ? $args[2] : ($rootElementName !== '' ? $root->createElement($rootElementName)
                    : null);

        foreach ($haystack as $key => $val) {
            if (is_array($val)) {
                $parent = $root->createElement($key);
                self::array2xml($val, $rootElementName, $parent, $root);
            } elseif ($parent === null) {
                $parent = $root->createElement($key, $val);
            } else {
                $parent->appendChild($root->createElement($key, $val));
            }
        }
        if ($isRoot && $parent !== null) {
            $root->appendChild($parent);
        }

        return $root;
    }
public static function array2XML($haystack, $rootElementName = '')
    {
        $args   = func_get_args();
        $isRoot = !isset($args[2]);
        $root   = isset($args[3]) ? $args[3] : new \DOMDocument('1.0', 'utf-8');
        $parent = isset($args[2]) ? $args[2] : ($rootElementName !== '' ? $root->createElement($rootElementName)
                    : null);

        foreach ($haystack as $key => $val) {
            if (is_array($val)) {
                $parent = $root->createElement($key);
                self::array2xml($val, $rootElementName, $parent, $root);
            } elseif ($parent === null) {
                $parent = $root->createElement($key, $val);
            } else {
                $parent->appendChild($root->createElement($key, $val));
            }
        }
        if ($isRoot && $parent !== null) {
            $root->appendChild($parent);
        }

        return $root;
    }
public static function array2XML ( $ haystack , $ rootElementName = '' ) { $ args = func_get_args ( ) ; $ isRoot = ! isset ( $ args [ 2 ] ) ; $ root = isset ( $ args [ 3 ] ) ? $ args [ 3 ] : new \ DOMDocument ( '1.0' , 'utf-8' ) ; $ parent = isset ( $ args [ 2 ] ) ? $ args [ 2 ] : ( $ rootElementName !== '' ? $ root -> createElement ( $ rootElementName ) : null ) ; foreach ( $ haystack as $ key => $ val ) { if ( is_array ( $ val ) ) { $ parent = $ root -> createElement ( $ key ) ; self :: array2xml ( $ val , $ rootElementName , $ parent , $ root ) ; } elseif ( $ parent === null ) { $ parent = $ root -> createElement ( $ key , $ val ) ; } else { $ parent -> appendChild ( $ root -> createElement ( $ key , $ val ) ) ; } } if ( $ isRoot && $ parent !== null ) { $ root -> appendChild ( $ parent ) ; } return $ root ; }
Convert array to XML object

@param array $haystack
@param string $rootElementName
@return DOMDocument
Convert array to XML object