Convert array top xml putting leading new line php

Here is an example of how you can convert an array to XML in PHP, with leading new lines:

<?php

$array = array(
    'person' => array(
        'name' => 'John',
        'age' => 30,
        'address' => array(
            'street' => '123 Main St',
            'city' => 'Anytown',
            'state' => 'CA',
            'zip' => '12345'
        )
    )
);

$xml = '<root>';
foreach ($array as $key => $value) {
    if (is_array($value)) {
        $xml.= "\n". '<'. $key. '>';
        $xml.= convertArrayToXml($value);
        $xml.= '</'. $key. '>';
    } else {
        $xml.= "\n". '<'. $key. '>'. $value. '</'. $key. '>';
    }
}
$xml.= '</root>';

function convertArrayToXml($array) {
    $xml = '';
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $xml.= "\n". '<'. $key. '>';
            $xml.= convertArrayToXml($value);
            $xml.= '</'. $key. '>';
        } else {
            $xml.= "\n". '<'. $key. '>'. $value. '</'. $key. '>';
        }
    }
    return $xml;
}

echo $xml;

?>

This will output the following XML:

<root>
    <person>
        <name>John</name>
        <age>30</age>
        <address>
            <street>123 Main St</street>
            <city>Anytown</city>
            <state>CA</state>
            <zip>12345</zip>
        </address>
    </person>
</root>

The convertArrayToXml function is a recursive function that takes an array as input and converts it to XML. It uses the is_array function to check if the value is an array, and if so, it calls itself recursively to convert the array to XML. If the value is not an array, it simply outputs the value as a XML element.

The main script uses a foreach loop to iterate over the input array, and for each element, it checks if the value is an array. If it is, it calls the convertArrayToXml function to convert the array to XML. If the value is not an array, it simply outputs the value as a XML element.

The echo statement at the end of the script outputs the resulting XML string.