This article mainly introduces the simple method of parsing xml into an array in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
The examples in this article describe the simplicity of PHP Implement the method of parsing xml into an array. Share it with everyone for your reference, the details are as follows:
Recently I want to make a plug-in mechanism, which requires the use of xml. When parsing xml, it needs to be converted into an array. I specially record this parsing method
xmlDemo.xml file:
<?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 code:
<?php $xmls=file_get_contents("xmlDemo.xml"); $xml =simplexml_load_string($xmls); $xmljson= json_encode($xml); $xml=json_decode($xmljson,true); var_dump($xml);
Running result:
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) { } }
Related recommendations:
##php implementation Parse web pages and download images to local
The above is the detailed content of PHP simply implements the method of parsing xml into an array. For more information, please follow other related articles on the PHP Chinese website!