php XML转数组,节点的name属性作为数组的key


如下XML

   
  <?xml version="1.0" encoding="gb2312"?>
  
<test>
<head>
<field name="version" pin="true">1.0</field>
<field name="type">111</field>
<field name="no">001</field>
<field name="code" pin="false">123456</field>
<field name="msg" pin="true">it is a message</field>
</head>
<body>
<field name="count">1</field>
<field-list name="array_001">
<field-list name="0">
<field name="FIELD_1"><![CDATA[1100000001]]></field>
<field name="FIELD_2">111111</field>
</field-list>
<field-list name="1">
<field name="FIELD_1"><![CDATA[测试]]></field>
<field name="FIELD_2">2222</field>
</field-list>
</field-list>
</body>
</test>

想转成如下格式的数组:

   
  array (
  
'head' => array (
'version' => '1.0',
'type' => '111',
'no' => '001',
'code' => '123456',
'msg' => 'it is a message',
),
'body' =>
array (
'count' => 1,
'array_001' =>
array (
0 => array (
FIELD_0 => '1100000001',
FIELD_1 => '1111111',
),
1 => array (
FIELD_0 => '测试',
FIELD_1 => '22222',
),
),
)
);

求指导...

xml 数组

鋼鉄D加鲁鲁 11 years, 2 months ago

Your Answer