Home >php教程 >PHP源码 >simplexml的对象转成数组

simplexml的对象转成数组

PHP中文网
PHP中文网Original
2016-05-25 17:14:381176browse

今天用到了simplexml来解析xml文件 去网站搜了很多直接把object数组对象转换成数组的方法 发现很多代码不好用
而且很多用到了递归,分享下代码

1. [代码][PHP]代码               

$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" } } }
*/

                   

                   

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