PHP中 xml转数组
来源:互联网 发布:maclc如何安装windows 编辑:程序博客网 时间:2024/03/29 19:30
<?php$simxml = simplexml_load_file('./book.xml');//print_r($sim);//把XML塞进数组中$arr = array();function xml2arr($sim){//如果是对象,则强转$arr = (array)$sim;//读强转后的数组做foreach循环foreach ($arr as $k => $v) {if ($v instanceof simplexmlElement || is_array ($v)) {$arr[$k] = xml2arr($v);}}return $arr;}print_r($xmlarr = xml2arr($simxml)) ;echo $xmlarr['book']['1']['@attributes']['category'];?>
一下是运行结果:
Array ( [book] => Array ( [0] => Array ( [@attributes] => Array ( [category] => COOKING ) [title] => Everyday Italian [author] => Giada De Laurentiis [year] => 2005 [price] => 30.00 ) [1] => Array ( [@attributes] => Array ( [category] => 武侠 ) [title] => 侠客行 [author] => 金庸 [year] => 2005 [price] => 29.99 ) [2] => Array ( [@attributes] => Array ( [category] => 网页 ) [title] => Jquery 7日通 [author] => 小二虎 [year] => 2003 [price] => 49.99 ) [3] => Array ( [@attributes] => Array ( [category] => 网页 ) [title] => Learning XML [author] => Erik T. Ray [year] => 2003 [price] => 39.95 [edition] => 第三版 ) ) ) 武侠
使用到的XML文件如下:
<?xml version ='1.0' encoding='utf8'?><bookstore><book category='COOKING'><title lang='en'>Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book><book category='武侠'><title lang='中文'>侠客行</title><author>金庸</author><year>2005</year><price>29.99</price></book><book category='网页'><title lang='中文'>Jquery 7日通</title><author>小二虎</author><year>2003</year><price>49.99</price></book><book category='网页'><title lang='en'>Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price><edition>第三版</edition></book></bookstore>
0 0
- PHP中 xml转数组
- php xml转数组
- php xml转数组
- ----php之XML转数组函数
- php 数组转xml 数组转json xml转数组 json转数组
- xml数据转数组
- xml转数组
- XML转数组
- xml数据转数组
- XML转数组
- php实现xml转数组[兼容性很强]
- php字符串转数组
- php 对象转数组
- php对象转数组
- php 对象转数组
- PHP中把对象转数组的几个方法
- xml转数组的方法
- php对像转数组
- ObjectARX学习笔记(十六)--如何设置CAD选项对话框的配置AcApProfileManagerReactor反应器
- P2000、MSA1040、MSA2040存储容量比较
- Linux挂载移动硬盘、U盘
- MFC对话框增加最大化和最小化按钮
- 携一缕清风静处
- PHP中 xml转数组
- 本土主帅纷纷下课
- Android中模拟post表单提交,带参数及文件参数
- 理解inode
- 微信封杀大量草根公众号
- solr中关于core的管理
- 转载和积累系列 - 内存碎片化问题
- dom4j解析xml字符串实例
- 异速联虚拟打印出来格式不对,如何处理