How to parse xml into a two-dimensional array through php
Source: Internet
Author: User
& Lt ;? Xmlversion = & quot; 1.0 & quot; encoding = & quot; UTF-8 & quot ;? & Gt; & lt; config & gt; & lt; db & gt; & lt; servicer & gt; localhost1 & lt;/servicer & gt; & lt; root & gt; root & lt;/root & gt; Localhost1 Root 123 Load ("config. xml "); $ roots = $ doc-> documentElement; // get the root node, that is, config (only one) $ childs = $ roots-> childNodes; // obtain all the child nodes under the root node, namely db smartyfor ($ I = 0; $ I <$ childs-> length; $ I ++) {// cyclically store the array $ config_item = $ childs-> item ($ I) according to the number of subnodes under the root node ); // Obtain the db smarty $ configs [$ config_item-> nodeName] = array (); // store the names of the db smarty subnodes as data in the array $ items = $ config_item-> childNodes; // Obtain all child nodes in the database smarty for ($ j = 0; $ j <$ items-> length; $ j ++) {// cyclically store the names and values of child nodes in db smarty into two-dimensional arrays according to the number of child nodes in db smarty $ item = $ items-> item ($ j ); // cyclically obtain the subnode $ configs [$ config_item-> nodeName] [$ item-> nodeName] = $ item-> nodeValue;} under each db smarty ;} // writing a two-dimensional array} var_dump ($ configs );
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.