How to implement XML transform arrays in PHP

Source: Internet
Author: User
This article mainly introduces the method of implementing XML transform array in PHP, and analyzes the operation skill of PHP operation XML format file to transform array with concrete instance form, involving the method of loading, traversing and transforming XML format data, and the need of friends can refer to

Specific as follows:

<?php$info = ' <?xml version= ' 1.0 ' encoding= ' utf-8 '?> <data> <GeocoderSearchResponse> <status>OK</status> <result> <location> <lat>39.94921 </lat> <lng>116.463619</lng> </location> <precise>0</pre        cise> <confidence>50</confidence> <level> Scripts </level> </result>          </GeocoderSearchResponse> <GeocoderSearchResponse> <status>OK</status> <result> <location> <lat>39</lat> &LT;LNG&GT;116&LT;/LNG > </location> <precise>0</precise> &LT;CONFIDENCE&GT;50&LT;/CONFIDENCE&G          T <level> script 123</level> </result> </GeocoderSearchResponse> </data> '; $xml = Simplexml_Load_string ($info), function Xml2array ($xmlobject) {if ($xmlobject) {foreach (array) $xmlobject as $k + = $v) { $data [$k] =!is_string ($v)?    Xml2array ($v): $v;  } return $data; }} $data = Xml2array ($xml); Var_dump ($data);? >

The results of the operation are as follows:

Array (1) {["Geocodersearchresponse"]=> Array (2) {  [0]=>  Array (2) {   ["status"]=>   string (2) "OK"   ["Result"]=>   Array (4) {    ["location"]=>    Array (2) {     ["lat"]=>     string (8) " 39.94921 "     [" LNG "]=>     string (Ten)" 116.463619 "    }    [" Precise "]=>    string (1)" 0 "    [" Confidence "]=>    string (2)"    ["Level"]=>    string (6) "script"}  }  [1]=>  Array (2) {   ["status"]=>   string (2) "OK"   ["Result"]=>   Array (4) {    ["location"]=>    Array (2) {     ["lat"]=>     string (2) "["]     ["LNG"]=>     string (3) "    " " Precise "]=>    string (1)" 0 "    [" Confidence "]=>    string (2)" "    [" Level "]=>    string (9 ) "Script 123"}}}  

The above is the whole content of this article, I hope that everyone's study has helped.


Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.