本文實(shí)例講述了PHP簡(jiǎn)單實(shí)現(xiàn)解析xml為數(shù)組的方法。分享給大家供大家參考,具體如下:
最近想要做一個(gè)插件機(jī)制,需要用到xml,在解析xml時(shí)候需要轉(zhuǎn)換為數(shù)組,特意記錄一個(gè)此種解析方式
xmlDemo.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <main xmlns="http://www.xiaoetongo.cn" versionCode="1.0"> <controller co="Aritles"> <meth style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";"><?php $xmls=file_get_contents("xmlDemo.xml"); $xml =simplexml_load_string($xmls); $xmljson= json_encode($xml); $xml=json_decode($xmljson,true); var_dump($xml);
運(yùn)行結(jié)果:
array(4) { ["@attributes"]=> array(1) { ["versionCode"]=> string(3) "1.0" } ["controller"]=> array(2) { [0]=> array(2) { ["@attributes"]=> array(1) { ["co"]=> string(7) "Aritles" } ["meth"]=> array(1) { ["@attributes"]=> array(2) { ["title"]=> string(12) "測(cè)試插件" ["do"]=> string(6) "aritle" } } } [1]=> array(2) { ["@attributes"]=> array(1) { ["co"]=> string(3) "Ari" } ["meth"]=> array(2) { [0]=> array(1) { ["@attributes"]=> array(2) { ["title"]=> string(12) "測(cè)試插件" ["do"]=> string(2) "ar" } } [1]=> array(1) { ["@attributes"]=> array(2) { ["title"]=> string(12) "測(cè)試插件" ["do"]=> string(1) "a" } } } } } ["install"]=> array(0) { } ["upgrade"]=> array(0) { } }
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。