首页 > 编程语言 > 详细

php数组转xml

时间:2015-11-02 10:22:56      阅读:132      评论:0      收藏:0      [点我收藏+]
$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>
*/

php数组转xml

原文:http://www.cnblogs.com/M-D-Luffy/p/4929359.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!