XML转换成数组方法

时间:2022-01-03 19:03:39
<?php



function xmlToArray2($xml) {
    // 将XML转为array
    $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    return $array_data;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <title>演示:最好用的XML转换成数组方法</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
        <link rel="stylesheet" type="text/css" href="http://www.sucaihuo.com/jquery/css/common.css" />
        <style type="text/css">
        </style>
    </head>
    <body>
        <div style="width:500px;margin:30px auto 0;font-size: 16px;">
            <p class="notice">提示:以微信推送海关为例的xml为例,xml在线转换数组</p>
            <pre>
            <?php
            $xml = "<xml>
   <appid>wx2421b1c4370ec43b</appid>
   <customs>ZHENGZHOU_BS</customs>
   <mch_customs_no>D00411</mch_customs_no>
   <mch_id>1262544101</mch_id>
   <order_fee>13110</order_fee>
   <out_trade_no>15112496832609</out_trade_no>
   <product_fee>13110</product_fee>
   <sign>8FF6CEF879FB9555CD580222E671E9D4</sign>
   <transaction_id>1006930610201511241751403478</transaction_id>
   <transport_fee>0</transport_fee>
   <fee_type>CNY</fee_type>
   <sub_order_no>15112496832609001</sub_order_no>
</xml>";
            var_dump(xmlToArray2($xml));
            ?>
            </pre>
        </div>
    </body>
</html>
<!-- 以下是统计及其他信息,与演示无关,不必理会 -->

 
<style type="text/css">
    .vad { margin: 250px 0 5px; font-family: Consolas,arial,宋体,sans-serif; text-align:center;}
    .vad a { display: inline-block; height: 36px; line-height: 36px; margin: 0 5px; padding: 0 50px; font-size: 14px;
text-align:center; color:#eee; text-decoration: none; background-color: #222;} .vad a:hover { color: #fff; background-color: #000;} .thead { width: 728px; height: 90px; margin: 0 auto; border-bottom: 40px solid #fff;} </style>