Home > Backend Development > PHP Tutorial > 问一个关于xml转php 数组的有关问题

问一个关于xml转php 数组的有关问题

WBOY
Release: 2016-06-13 12:39:11
Original
935 people have browsed it

问一个关于xml转php 数组的问题
我现在要接收一个xml的内容,不是文件
接收的文件内容如下格式

?

<ax>0000</ax><at>ax0000111222333442</at><ab>12345</ab><an>测试</an><ao>4021000028853283</ao><ad>20130828</ad><pmemo1>备注1</pmemo1><pmemo2>备注2</pmemo2><pmemo3>备注3</pmemo3></preq><br> <br> <br> 怎么直接把这段代码转换为php数组,这个不是个文件,是个变量<br> <br> </p> <div class="topic-extra-info"> <div class="tag"> <span>xml</span> <span>php</span> <div class="social-share"> <span class="prompt">分享到:</span> <span class="social-share-buttons" data-title='【问一个关于xml转php 数组的问题】我现在要接收一个xml的内容,不是文件接收的文件内容如下格式?<?xml?version="1.0"?encoding="utf-8"?><pReq><ax>0000</ax...' data-pics=""></span> <br><font color="#FF8000">------解决方案--------------------</font><br>找一个xml_to_array的函数 比如 <br> <br> http://www.cnblogs.com/heiing/archive/2009/12/31/1637015.html<br> <br> 然后使用它<br> <br> <pre class="brush: php"><br> function xml_to_array( $xml )<br> {<br>     $reg = "/]*?>([\\x00-\\xFF]*?)/";<br>     if(preg_match_all($reg, $xml, $matches))<br>     {<br>         $count = count($matches[0]);<br>         $arr = array();<br>         for($i = 0; $i          {<br>             $key = $matches[1][$i];<br>             $val = xml_to_array( $matches[2][$i] );  // 递归<br>             if(array_key_exists($key, $arr))<br>             {<br>                 if(is_array($arr[$key]))<br>                 {<br>                     if(!array_key_exists(0,$arr[$key]))<br>                     {<br>                         $arr[$key] = array($arr[$key]); <div class="clear"> </div>
Copy after login
Related labels:
source:php.cn
Previous article: 关于利用json实现java与php间的传值机制有关问题 Next article: mysqli执行mysql事务的有关问题
Statement of this Website
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template