如何在php中将XML转换为数组?我想将下面的XML转换为PHP数组。对我怎么做有什么建议吗?<aaaa Version="1.0">
<bbb>
<cccc>
<dddd Id="id:pass" />
<eeee name="hearaman" age="24" />
</cccc>
</bbb></aaaa>
3 回答
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);$json = json_encode($xml);$array = json_decode($json,TRUE);
料青山看我应如是
TA贡献1772条经验 获得超8个赞
$buffer
function XML2Array(SimpleXMLElement $parent){ $array = array(); foreach ($parent as $name => $element) { ($node = & $array[$name]) && (1 === count($node) ? $node = array($node) : 1) && $node = & $node[]; $node = $element->count() ? XML2Array($element) : trim($element); } return $array;}$xml = simplexml_load_string($buffer);$array = XML2Array($xml);$array = array($xml->getName() => $array);
Array( [aaaa] => Array ( [bbb] => Array ( [cccc] => Array ( [dddd] => [eeee] => ) ) ))
$xml = simplexml_load_string($buffer);$array = json_decode(json_encode((array) $xml), true);$array = array($xml->getName() => $array);
Array( [aaaa] => Array ( [@attributes] => Array ( [Version] => 1.0 ) [bbb] => Array ( [cccc] => Array ( [dddd] => Array ( [@attributes] => Array ( [Id] => id:pass ) ) [eeee] => Array ( [@attributes] => Array ( [name] => hearaman [age] => 24 ) ) ) ) ))
- 3 回答
- 0 关注
- 679 浏览
添加回答
举报
0/150
提交
取消