PHP把相对复杂结构的XML转换成数组。

这个问题纠结了半上午,看网上也没有什么特别有帮助的文章,我就发出来和大家共享一下吧。

<?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] => 杩欐槸涓€涓狟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] => 鍗曢€?
)

[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中有属性,有子节点,当也可以是更复杂的结构,可以以些类推。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值