>백엔드 개발 >PHP 튜토리얼 >PHP解析XML转成数组

PHP解析XML转成数组

WBOY
WBOY원래의
2016-06-13 10:20:25760검색

PHP解析XML转成数组 在线等

XML code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?xml version="1.0" encoding="UTF-8"?><root>   <a>      <b name="name" value="value"></b>      <b name="name" value="value"></b>      <b name="name" value="value"></b>   </a>   <a>      <b name="name" value="value"></b>      <b name="name" value="value"></b>         </a>   <a>      <b name="name" value="value"></b>      <b name="name" value="value"></b>      <b name="name" value="value"></b>     <b name="name" value="value"></b>   </a></root>

  解析成数组:
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->    Array(       [0]=>Array(            [0]=>Array(name=>'name',value=>'value'),            [1]=>Array(name=>'name',value=>'value'),            [2]=>Array(name=>'name',value=>'value')       ),       [1]=>Array(         [0]=>Array(name=>'name',value=>'value'),         [1]=>Array(name=>'name',value=>'value'),         [2]=>Array(name=>'name',value=>'value')       ),       [2]=>Array(         [0]=>Array(name=>'name',value=>'value'),         [1]=>Array(name=>'name',value=>'value'),         [2]=>Array(name=>'name',value=>'value')      ),)


------解决方案--------------------
PHP code
<?php $xml=<<<XML<?xml version="1.0" encoding="UTF-8"?><root>   <a>      <b name="name" value="value"></b>      <b name="name" value="value"></b>      <b name="name" value="value"></b>   </a>   <a>      <b name="name" value="value"></b>      <b name="name" value="value"></b>         </a>   <a>      <b name="name" value="value"></b>      <b name="name" value="value"></b>      <b name="name" value="value"></b>     <b name="name" value="value"></b>   </a></root>XML;$obj=simplexml_load_string($xml);foreach($obj as $k=>$v){    $temp=array();    foreach ($v->b as $sv) {        $a=(array)$sv->attributes();        $temp[]=$a[[email protected]'];    }    $arr[]=$temp;}print_r($arr);<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code
$s =<root>   <a>      <b name="name" value="value"></b>      <b name="name" value="value"></b>      <b name="name" value="value"></b>   </a>   <a>      <b name="name" value="value"></b>      <b name="name" value="value"></b>   </a>   <a>      <b name="name" value="value"></b>      <b name="name" value="value"></b>      <b name="name" value="value"></b>     <b name="name" value="value"></b>   </a></root>XML;$xml = simplexml_load_string($s);foreach($xml as $item1) {  $r = array();  foreach($item1 as $item2) {    $t = array();    foreach($item2->attributes() as $k=>$v) {      $t[] = array($k => "$v");    }    $r[] = $t;  }  $res[] = $r;}print_r($res);<div class="clear">
                 
              
              
        
            </div>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.