Skip to content

common arrayToXML

James Cobban edited this page Mar 28, 2022 · 3 revisions

function arrayToXML($array, $options)

Up: PHP Global Environment

This function is used by scripts which generate eXtended Markup Language (XML) documents to convert a PHP array or object to an XML document. It takes the following parameters:

parameter purpose
$array a PHP array or object which defines the meaning of the XML document to be returned
$options an optional associative array of options customizing the output

and returns a string containing an XML document.

If specified the $options may contain the following settings:

name contents
'nodename' A string providing the name of the XML tag enclosing the response
'indent' A string, usually of spaces, that is used in front of the opening and closing tags of the result. The contents of the tag are further indented by four spaces. If 'indent' is omitted or is set to the empty string then the method uses the PHP header function to identify the document as XML and includes the XML format declaration at the front of the returned string.

If $array is a string, then the returned value is the value of $array enclosed in an XML tag. For example

    arrayToXML('data',array('nodename' => 'tagname'));`

returns:

    <tagname>
        data
    </tagname>

If $array is an associative array, or an object, then the returned string contains the elements of the array or object as sub-tags. To support the case of multiple sub-tags with the same tag name, you specify the sub-tagname and the value in a sub-array because associative arrays require the keys to be unique. For example:

    $array     = array('key1' => 'value 1',
                       'key2' => 'value 2',
                       array('common' => 'value 3'),
                       array('common' => 'value 4'));
    arrayToXML($array,array('nodename' => 'tagname'));`

returns:

    <tagname>
        <key1>value 1</key1>
        <key2>value 2</key2>
        <common>value 3</common>
        <common>value 4</common>
    </tagname>

The value of an key in an array may be another array, in which case the functionality is recursive. For example:

    $array     = array('key1' => array('low1' => 'val1', 'low2' => 'val2'),
                       'key2' => array('low3' => 'val3', 'low4' => 'val4'),
                       array('common' => array('low5' => 'val5', 'low6' => 'val6')),
                       array('common' => array('low7' => 'val7', 'low8' => 'val8'));
    arrayToXML($array,array('nodename' => 'tagname'));`

returns:

    <tagname>
        <key1>
            <low1>val1</low1>    
            <low2>val2</low2>    
        </key1>
        <key2>
            <low3>val3</low3>    
            <low4>val4</low4>    
        </key2>
        <common>
            <low5>val5</low5>    
            <low6>val6</low6>    
        </common>
        <common>
            <low7>val7</low7>    
            <low8>val8</low8>    
        </common>
    </tagname>

Next: function arrayToXJSON($array, $options)

Clone this wiki locally