Home  >  Article  >  Backend Development  >  How to implement xml conversion array in php

How to implement xml conversion array in php

墨辰丷
墨辰丷Original
2018-05-26 11:38:441377browse

This article mainly introduces the method of converting xml arrays in PHP, and analyzes the relevant operating techniques of PHP in operating xml format files to convert arrays in the form of specific examples, involving methods such as loading, traversing, and converting xml format data. Friends in need can refer to

. The details are as follows:

<?php
$info = &#39;<?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</precise>
          <confidence>50</confidence>
          <level>脚本</level>
          </result>
        </GeocoderSearchResponse>
        <GeocoderSearchResponse>
          <status>OK</status>
          <result>
            <location>
              <lat>39</lat>
              <lng>116</lng>
            </location>
          <precise>0</precise>
          <confidence>50</confidence>
          <level>脚本123</level>
          </result>
        </GeocoderSearchResponse>
      </data>&#39;;
$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 running results 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(10) "116.463619"
    }
    ["precise"]=>
    string(1) "0"
    ["confidence"]=>
    string(2) "50"
    ["level"]=>
    string(6) "脚本"
   }
  }
  [1]=>
  array(2) {
   ["status"]=>
   string(2) "OK"
   ["result"]=>
   array(4) {
    ["location"]=>
    array(2) {
     ["lat"]=>
     string(2) "39"
     ["lng"]=>
     string(3) "116"
    }
    ["precise"]=>
    string(1) "0"
    ["confidence"]=>
    string(2) "50"
    ["level"]=>
    string(9) "脚本123"
   }
  }
 }
}

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

give all the one-dimensional array in the two-dimensional array in php Methods of adding values

How to operate json objects and arraysKey value case conversion

PHP Implementation Judgment Array Is one-dimensional, two-dimensional or several-dimensional method

The above is the detailed content of How to implement xml conversion array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn