设为首页 收藏本站
查看: 957|回复: 0

[经验分享] PHP把相对复杂结构的XML转换成数组。

[复制链接]

尚未签到

发表于 2017-4-10 07:11:48 | 显示全部楼层 |阅读模式
这个问题纠结了半上午,看网上也没有什么特别有帮助的文章,我就发出来和大家共享一下吧。

<?php
$xml = simplexml_load_file ( "data.xml" );
$result = $xml->xpath ( "Control" );
foreach($result as $key=>$r)
{
unset($a);
unset($pro_attr);
unset($pro_a);
$a[ID]=(int)$r[ID];
$a[Type]=(string)$r[Type];
foreach($r->Property as $k=>$p)
{
$pro_a[Name]=(string)$p[Name];
$pro_a[Type]=(string)$p[Type];
$pro_a[Value]=(string)$p[Value];
$pro_attr[(string)$p[Name]]=$pro_a;
}
$a[property]=$pro_attr;
$attr[]=$a;
}
print_r($attr);
?>

本来呢,用ECHO输出那个$return的项结果得出来 的值 是正确的,以为取出来本来就是数组,但用print_r打印输出发现都是simplexmlelement object对象,这不是我想要的,我想要的是这样的

Array
(
[0] => Array
(
[ID] => 1
[Type] => CCTextBox
[property] => Array
(
[CCId] => Array
(
[Name] => CCId
[Type] => CCString
[Value] => TextBox1
)
[CCTop] => Array
(
[Name] => CCTop
[Type] => CCInteger
[Value] => 60
)
[CCLeft] => Array
(
[Name] => CCLeft
[Type] => CCInteger
[Value] => 200
)
[CCWidth] => Array
(
[Name] => CCWidth
[Type] => CCInteger
[Value] => 454
)
[CCHeight] => Array
(
[Name] => CCHeight
[Type] => CCInteger
[Value] => 21
)
[CCPassWordChar] => Array
(
[Name] => CCPassWordChar
[Type] => CCString
[Value] =>
)
[CCEnabled] => Array
(
[Name] => CCEnabled
[Type] => CCInteger
[Value] => 1
)
[CCMaxLength] => Array
(
[Name] => CCMaxLength
[Type] => CCInteger
[Value] =>
)
[CCBackColor] => Array
(
[Name] => CCBackColor
[Type] => CCInteger
[Value] => #0000ff
)
[CCForeColor] => Array
(
[Name] => CCForeColor
[Type] => CCInteger
[Value] =>
)
[CCTextAlign] => Array
(
[Name] => CCTextAlign
[Type] => CCInteger
[Value] =>
)
[CCTabIndex] => Array
(
[Name] => CCTabIndex
[Type] => CCInteger
[Value] => 0
)
)
)
[1] => Array
(
[ID] => 2
[Type] => CCButton
[property] => Array
(
[CCId] => Array
(
[Name] => CCId
[Type] => CCString
[Value] => Button1
)
[CCTop] => Array
(
[Name] => CCTop
[Type] => CCInteger
[Value] => 100
)
[CCLeft] => Array
(
[Name] => CCLeft
[Type] => CCInteger
[Value] => 200
)
[CCWidth] => Array
(
[Name] => CCWidth
[Type] => CCInteger
[Value] => 550
)
[CCHeight] => Array
(
[Name] => CCHeight
[Type] => CCInteger
[Value] => 24
)
[CCEnabled] => Array
(
[Name] => CCEnabled
[Type] => CCInteger
[Value] => 1
)
[CCBackColor] => Array
(
[Name] => CCBackColor
[Type] => CCInteger
[Value] =>
)
[CCForeColor] => Array
(
[Name] => CCForeColor
[Type] => CCInteger
[Value] =>
)
[CCText] => Array
(
[Name] => CCText
[Type] => CCString
[Value] => 杩欐槸涓&#8364;涓狟UTTON
)
[CCTabIndex] => Array
(
[Name] => CCTabIndex
[Type] => CCInteger
[Value] => 0
)
)
)
[2] => Array
(
[ID] => 3
[Type] => CCLabel
[property] => Array
(
[CCId] => Array
(
[Name] => CCId
[Type] => CCString
[Value] => Label1
)
[CCTop] => Array
(
[Name] => CCTop
[Type] => CCInteger
[Value] => 140
)
[CCLeft] => Array
(
[Name] => CCLeft
[Type] => CCInteger
[Value] => 200
)
[CCWidth] => Array
(
[Name] => CCWidth
[Type] => CCInteger
[Value] => 100
)
[CCHeight] => Array
(
[Name] => CCHeight
[Type] => CCInteger
[Value] => 20
)
[CCEnabled] => Array
(
[Name] => CCEnabled
[Type] => CCInteger
[Value] => 1
)
[CCBackColor] => Array
(
[Name] => CCBackColor
[Type] => CCInteger
[Value] =>
)
[CCForeColor] => Array
(
[Name] => CCForeColor
[Type] => CCInteger
[Value] =>
)
[CCText] => Array
(
[Name] => CCText
[Type] => CCString
[Value] => 鏂囨湰
)
[CCTabIndex] => Array
(
[Name] => CCTabIndex
[Type] => CCInteger
[Value] => 0
)
)
)
[3] => Array
(
[ID] => 4
[Type] => CCComboBox
[property] => Array
(
[CCId] => Array
(
[Name] => CCId
[Type] => CCString
[Value] => ComboBox1
)
[CCTop] => Array
(
[Name] => CCTop
[Type] => CCInteger
[Value] => 180
)
[CCLeft] => Array
(
[Name] => CCLeft
[Type] => CCInteger
[Value] => 200
)
[CCWidth] => Array
(
[Name] => CCWidth
[Type] => CCInteger
[Value] => 154
)
[CCHeight] => Array
(
[Name] => CCHeight
[Type] => CCInteger
[Value] => 21
)
[CCEnabled] => Array
(
[Name] => CCEnabled
[Type] => CCInteger
[Value] => 1
)
[CCBackColor] => Array
(
[Name] => CCBackColor
[Type] => CCInteger
[Value] =>
)
[CCForeColor] => Array
(
[Name] => CCForeColor
[Type] => CCInteger
[Value] =>
)
[CCText] => Array
(
[Name] => CCText
[Type] => CCString
[Value] => 涓嬫媺鍒?
)
[CCTabIndex] => Array
(
[Name] => CCTabIndex
[Type] => CCInteger
[Value] => 0
)
[CCValues] => Array
(
[Name] => CCValues
[Type] => CCString
[Value] =>
)
)
)
[4] => Array
(
[ID] => 5
[Type] => CCCheckBox
[property] => Array
(
[CCId] => Array
(
[Name] => CCId
[Type] => CCString
[Value] => CheckBox1
)
[CCTop] => Array
(
[Name] => CCTop
[Type] => CCInteger
[Value] => 220
)
[CCLeft] => Array
(
[Name] => CCLeft
[Type] => CCInteger
[Value] => 200
)
[CCWidth] => Array
(
[Name] => CCWidth
[Type] => CCInteger
[Value] => 20
)
[CCHeight] => Array
(
[Name] => CCHeight
[Type] => CCInteger
[Value] => 20
)
[CCEnabled] => Array
(
[Name] => CCEnabled
[Type] => CCInteger
[Value] => 1
)
[CCForeColor] => Array
(
[Name] => CCForeColor
[Type] => CCInteger
[Value] =>
)
[CCText] => Array
(
[Name] => CCText
[Type] => CCString
[Value] => 鍗曢&#8364;?
)
[CCTabIndex] => Array
(
[Name] => CCTabIndex
[Type] => CCInteger
[Value] => 0
)
[CCValues] => Array
(
[Name] => CCValues
[Type] => CCString
[Value] =>
)
)
)
[5] => Array
(
[ID] => 6
[Type] => CCRadioButton
[property] => Array
(
[CCId] => Array
(
[Name] => CCId
[Type] => CCString
[Value] => RadioButton1
)
[CCTop] => Array
(
[Name] => CCTop
[Type] => CCInteger
[Value] => 260
)
[CCLeft] => Array
(
[Name] => CCLeft
[Type] => CCInteger
[Value] => 200
)
[CCWidth] => Array
(
[Name] => CCWidth
[Type] => CCInteger
[Value] => 20
)
[CCHeight] => Array
(
[Name] => CCHeight
[Type] => CCInteger
[Value] => 20
)
[CCEnabled] => Array
(
[Name] => CCEnabled
[Type] => CCInteger
[Value] => 1
)
[CCTabIndex] => Array
(
[Name] => CCTabIndex
[Type] => CCInteger
[Value] => 0
)
)
)
[6] => Array
(
[ID] => 7
[Type] => CCImage
[property] => Array
(
[CCId] => Array
(
[Name] => CCId
[Type] => CCString
[Value] => Image1
)
[CCTop] => Array
(
[Name] => CCTop
[Type] => CCInteger
[Value] => 300
)
[CCLeft] => Array
(
[Name] => CCLeft
[Type] => CCInteger
[Value] => 200
)
[CCWidth] => Array
(
[Name] => CCWidth
[Type] => CCInteger
[Value] => 28
)
[CCHeight] => Array
(
[Name] => CCHeight
[Type] => CCInteger
[Value] => 30
)
[CCEnabled] => Array
(
[Name] => CCEnabled
[Type] => CCInteger
[Value] => 1
)
[CCTabIndex] => Array
(
[Name] => CCTabIndex
[Type] => CCInteger
[Value] => 0
)
[CCSrc] => Array
(
[Name] => CCSrc
[Type] => CCInteger
[Value] =>
)
)
)
[7] => Array
(
[ID] => 8
[Type] => CCTime
[property] => Array
(
[CCId] => Array
(
[Name] => CCId
[Type] => CCString
[Value] => Time1
)
[CCTop] => Array
(
[Name] => CCTop
[Type] => CCInteger
[Value] => 300
)
[CCLeft] => Array
(
[Name] => CCLeft
[Type] => CCInteger
[Value] => 200
)
[CCWidth] => Array
(
[Name] => CCWidth
[Type] => CCInteger
[Value] => 154
)
[CCHeight] => Array
(
[Name] => CCHeight
[Type] => CCInteger
[Value] => 21
)
[CCEnabled] => Array
(
[Name] => CCEnabled
[Type] => CCInteger
[Value] => 1
)
[CCTabIndex] => Array
(
[Name] => CCTabIndex
[Type] => CCInteger
[Value] => 0
)
[CCFormat] => Array
(
[Name] => CCFormat
[Type] => CCInteger
[Value] =>
)
)
)
)


终于想到了转型,即然输出的是正确的那么我们利用PHP的弱数据类型 的特点把它转型行不行呢?试了一下,果不其然。然后用循环把它输入到数组中保存,结果就是这样的,这种XML中有属性,有子节点,当也可以是更复杂的结构,可以以些类推。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362607-1-1.html 上篇帖子: PHP数组键值双引号单引号区别 下篇帖子: [转]PHP 关于文件上传下载 断点续传问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表