Home > php教程 > php手册 > simplexml的对象转成数组

simplexml的对象转成数组

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:32:10
Original
924 people have browsed it

第一次分享代码,今天用到了simplexml来解析xml文件去网站搜了很多直接把object数组对象转换成数组的方法发现很多代码不好用 而且很多用到了递归,分享下我的代码 无 $xml =simplexml_load_file('Public/syn.xml');/*此处打印出来的结果是object(SimpleXMLEle

第一次分享代码,今天用到了simplexml来解析xml文件 去网站搜了很多直接把object数组对象转换成数组的方法 发现很多代码不好用
而且很多用到了递归,分享下我的代码
$xml =simplexml_load_file('Public/syn.xml');
/*
此处打印出来的结果是
object(SimpleXMLElement)#20 (1) { ["server"]=> array(2) { [0]=> object(SimpleXMLElement)#26 (2) { ["server_name"]=> array(2) { [0]=> string(3) "777" [1]=> string(6) "123456" } ["server_ip"]=> string(13) "xxx.xxx.xxx.xxx" } [1]=> object(SimpleXMLElement)#27 (2) { ["server_name"]=> array(2) { [0]=> string(3) "888" [1]=> string(6) "123456" } ["server_ip"]=> string(13) "xxx.xxx.xxx.xxx" } } }
*/

$json_xml=json_encode($xml);
$dejson_xml=json_decode($json_xml,true);
/*
此处打印出来的结果是
array(1) { ["server"]=> array(2) { [0]=> array(2) { ["server_name"]=> array(2) { [0]=> string(3) "777" [1]=> string(6) "123456" } ["server_ip"]=> string(13) "xxx.xxx.xxx.xxx" } [1]=> array(2) { ["server_name"]=> array(2) { [0]=> string(3) "888" [1]=> string(6) "123456" } ["server_ip"]=> string(13) "xxx.xxx.xxx.xxx" } } }
*/


Copy after login
Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template