src/XmlConverter.php
public function addArrayAsChildren(array $array, SimpleXMLElement $xmlElement, $ignoreEmptyElements = true)
    {
        // Keep an array of children that are added to $xmlElement
        $children = [];

        // Add each element of the array as a child to $xmlElement
        foreach ($array as $key => $value) {
            // Ignore empty array elements
            if ($this->isEmpty($value) && $ignoreEmptyElements) {
                continue;
            }

            // Specifies attributes as a key/value array
            if ($key === '@attributes') {
                if (!is_array($value)) {
                    throw new Exception('@attributes must be an array');
                }

                foreach ($value as $attributeName => $attributeValue) {
                    $xmlElement->addAttribute($attributeName, htmlspecialchars($attributeValue));
                }

                continue;
            }

            // Special case to be able to set the value of an element to a string if the attribute is also being set,
            // without this a child element would be created instead.
            // The really hacky part is that setting the 0 key element overrides the value (it doesn't normally exist).
            if ($key === '@value') {
                if (!is_scalar($value)) {
                    throw new Exception('@value must be a scalar');
                }

                $xmlElement[0] = htmlspecialchars($value);
                continue;
            }

            // The value contains sub elements.
            // If the array is numeric then there are multiple of the same element.
            if (is_array($value)) {
                $useSubArray = key($value) === 0;
                $subValues = $useSubArray ? $value : [$value];

                $subChildren = [];
                foreach ($subValues as $subValue) {
                    // Recursively add child elements.
                    if (is_array($subValue)) {
                        $subChild = $xmlElement->addChild($key);
                        $this->addArrayAsChildren($subValue, $subChild, $ignoreEmptyElements);
                    } else {
                        $subChild = $xmlElement->addChild($key, $subValue);
                    }
                    $subChildren[] = $subChild;
                }

                $children[$key] = $useSubArray ? $subChildren : $subChildren[0];
                continue;
            }

            // Encode the value correctly
            $value = htmlspecialchars($value);
            $children[$key] = $xmlElement->addChild($key, $value);
        }

        return $children;
    }
public function addArrayAsChildren(array $array, SimpleXMLElement $xmlElement, $ignoreEmptyElements = true)
    {
        // Keep an array of children that are added to $xmlElement
        $children = [];

        // Add each element of the array as a child to $xmlElement
        foreach ($array as $key => $value) {
            // Ignore empty array elements
            if ($this->isEmpty($value) && $ignoreEmptyElements) {
                continue;
            }

            // Specifies attributes as a key/value array
            if ($key === '@attributes') {
                if (!is_array($value)) {
                    throw new Exception('@attributes must be an array');
                }

                foreach ($value as $attributeName => $attributeValue) {
                    $xmlElement->addAttribute($attributeName, htmlspecialchars($attributeValue));
                }

                continue;
            }

            // Special case to be able to set the value of an element to a string if the attribute is also being set,
            // without this a child element would be created instead.
            // The really hacky part is that setting the 0 key element overrides the value (it doesn't normally exist).
            if ($key === '@value') {
                if (!is_scalar($value)) {
                    throw new Exception('@value must be a scalar');
                }

                $xmlElement[0] = htmlspecialchars($value);
                continue;
            }

            // The value contains sub elements.
            // If the array is numeric then there are multiple of the same element.
            if (is_array($value)) {
                $useSubArray = key($value) === 0;
                $subValues = $useSubArray ? $value : [$value];

                $subChildren = [];
                foreach ($subValues as $subValue) {
                    // Recursively add child elements.
                    if (is_array($subValue)) {
                        $subChild = $xmlElement->addChild($key);
                        $this->addArrayAsChildren($subValue, $subChild, $ignoreEmptyElements);
                    } else {
                        $subChild = $xmlElement->addChild($key, $subValue);
                    }
                    $subChildren[] = $subChild;
                }

                $children[$key] = $useSubArray ? $subChildren : $subChildren[0];
                continue;
            }

            // Encode the value correctly
            $value = htmlspecialchars($value);
            $children[$key] = $xmlElement->addChild($key, $value);
        }

        return $children;
    }
public function addArrayAsChildren ( array $ array , SimpleXMLElement $ xmlElement , $ ignoreEmptyElements = true ) { // Keep an array of children that are added to $xmlElement $ children = [ ] ; // Add each element of the array as a child to $xmlElement foreach ( $ array as $ key => $ value ) { // Ignore empty array elements if ( $ this -> isEmpty ( $ value ) && $ ignoreEmptyElements ) { continue ; } // Specifies attributes as a key/value array if ( $ key === '@attributes' ) { if ( ! is_array ( $ value ) ) { throw new Exception ( '@attributes must be an array' ) ; } foreach ( $ value as $ attributeName => $ attributeValue ) { $ xmlElement -> addAttribute ( $ attributeName , htmlspecialchars ( $ attributeValue ) ) ; } continue ; } // Special case to be able to set the value of an element to a string if the attribute is also being set, // without this a child element would be created instead. // The really hacky part is that setting the 0 key element overrides the value (it doesn't normally exist). if ( $ key === '@value' ) { if ( ! is_scalar ( $ value ) ) { throw new Exception ( '@value must be a scalar' ) ; } $ xmlElement [ 0 ] = htmlspecialchars ( $ value ) ; continue ; } // The value contains sub elements. // If the array is numeric then there are multiple of the same element. if ( is_array ( $ value ) ) { $ useSubArray = key ( $ value ) === 0 ; $ subValues = $ useSubArray ? $ value : [ $ value ] ; $ subChildren = [ ] ; foreach ( $ subValues as $ subValue ) { // Recursively add child elements. if ( is_array ( $ subValue ) ) { $ subChild = $ xmlElement -> addChild ( $ key ) ; $ this -> addArrayAsChildren ( $ subValue , $ subChild , $ ignoreEmptyElements ) ; } else { $ subChild = $ xmlElement -> addChild ( $ key , $ subValue ) ; } $ subChildren [ ] = $ subChild ; } $ children [ $ key ] = $ useSubArray ? $ subChildren : $ subChildren [ 0 ] ; continue ; } // Encode the value correctly $ value = htmlspecialchars ( $ value ) ; $ children [ $ key ] = $ xmlElement -> addChild ( $ key , $ value ) ; } return $ children ; }
Convert an array into XML and adds the whole structure as a child of the existing SimpleXMLElement given.
An array of inserted children is returned so you can add further children to them if necessary.

@param array $array
@param SimpleXMLElement $xmlElement
@param bool $ignoreEmptyElements
@return SimpleXMLElement[]|array
Convert an array into XML and adds the whole structure as a child of the existing SimpleXMLElement given . An array of inserted children is returned so you can add further children to them if necessary .