时间:2021-07-01 10:21:17 帮助过:15人阅读
最近想要做一个插件机制,需要用到xml,在解析xml时候需要转换为数组,特意记录一个此种解析方式
xmlDemo.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <main xmlns="http://www.xiaoetongo.cn" versionCode="1.0"> <controller co="Aritles"> <meth title="测试插件" do="aritle"/> </controller> <controller co="Ari"> <meth title="测试插件" do="ar"/> <meth title="测试插件" do="a"/> </controller> <install><![CDATA[]]></install> <upgrade><![CDATA[]]></upgrade> </main>
php代码:
<?php
$xmls=file_get_contents("xmlDemo.xml");
$xml =simplexml_load_string($xmls);
$xmljson= json_encode($xml);
$xml=json_decode($xmljson,true);
var_dump($xml);运行结果:
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) "测试插件"
["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) "测试插件"
["do"]=>
string(2) "ar"
}
}
[1]=>
array(1) {
["@attributes"]=>
array(2) {
["title"]=>
string(12) "测试插件"
["do"]=>
string(1) "a"
}
}
}
}
}
["install"]=>
array(0) {
}
["upgrade"]=>
array(0) {
}
}相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
PHP计算特大数字的整数集
php使用curl仿制ip与refer步骤详解
以上就是PHP实现解析xml为数组案例详解的详细内容,更多请关注Gxl网其它相关文章!