php数组转xml

$ar array
    "auth" => array
        "user" => "customer",
        "password" => "password",
        "context" => "4",
    ),
    "owner" => array
        "user" => "customer2",
        "context" => "4",
    ),
    "language" => "en",
    "task" => array
        "code" => "0130",
    ), 
);
$xml = simplexml_load_string('<request />');
create($ar$xml);
echo $xml->saveXML();
 
function create($ar$xml) {
    foreach($ar as $k=>$v) {
        if(is_array($v)) {
            $x $xml->addChild($k);
            create($v$x);
        }else $xml->addChild($k$v);
    }
}
 
/* 结果显示
<?xml version="1.0" ?> 
<request>
    <auth>
        <user>customer</user> 
        <password>password</password> 
        <context>4</context> 
    </auth>
    <owner>
        <user>customer2</user> 
        <context>4</context> 
    </owner>
    <language>en</language> 
    <task>
        <code>0130</code> 
    </task>
</request>
*/
原文地址:https://www.cnblogs.com/M-D-Luffy/p/4929359.html