WordPress at Your Fingertips
function is not described

WC_API_XML_Handler::array_to_xml() private WC 2.1

Convert array into XML by recursively generating child elements

{} It's a method of the class: WC_API_XML_Handler{}

No Hooks.

Return

String. - generated XML

Usage

// private - for code of main (parent) class only
$result = $this->array_to_xml( $element_key, $element_value );
$element_key(string|array) (required)
- name for element, e.g. <OrderID>
$element_value(string|array)
- value for element, e.g. 1234
Default: array()

Changelog

Since 2.1 Introduced.

Code of WC_API_XML_Handler::array_to_xml() WC 5.6.0

private function array_to_xml( $element_key, $element_value = array() ) {

	if ( is_array( $element_value ) ) {

		// handle attributes
		if ( '@attributes' === $element_key ) {
			foreach ( $element_value as $attribute_key => $attribute_value ) {

				$this->xml->startAttribute( $attribute_key );
				$this->xml->text( $attribute_value );
				$this->xml->endAttribute();
			}
			return;
		}

		// handle multi-elements (e.g. multiple <Order> elements)
		if ( is_numeric( key( $element_value ) ) ) {

			// recursively generate child elements
			foreach ( $element_value as $child_element_key => $child_element_value ) {

				$this->xml->startElement( $element_key );

				foreach ( $child_element_value as $sibling_element_key => $sibling_element_value ) {
					$this->array_to_xml( $sibling_element_key, $sibling_element_value );
				}

				$this->xml->endElement();
			}
		} else {

			// start root element
			$this->xml->startElement( $element_key );

			// recursively generate child elements
			foreach ( $element_value as $child_element_key => $child_element_value ) {
				$this->array_to_xml( $child_element_key, $child_element_value );
			}

			// end root element
			$this->xml->endElement();
		}
	} else {

		// handle single elements
		if ( '@value' == $element_key ) {

			$this->xml->text( $element_value );

		} else {

			// wrap element in CDATA tags if it contains illegal characters
			if ( false !== strpos( $element_value, '<' ) || false !== strpos( $element_value, '>' ) ) {

				$this->xml->startElement( $element_key );
				$this->xml->writeCdata( $element_value );
				$this->xml->endElement();

			} else {

				$this->xml->writeElement( $element_key, $element_value );
			}
		}

		return;
	}
}