【发布时间】:2010-10-24 12:11:10
【问题描述】:
我需要递归地将 PHP SimpleXMLObject 转换为数组。问题是每个子元素也是一个 PHP SimpleXMLElement。
这可能吗?
【问题讨论】:
我需要递归地将 PHP SimpleXMLObject 转换为数组。问题是每个子元素也是一个 PHP SimpleXMLElement。
这可能吗?
【问题讨论】:
json_decode(json_encode((array) simplexml_load_string($obj)), 1);
【讨论】:
<?php $xml = '<?xml version="1.0" encoding="utf-8"?><data><empty/></data>'; $array = json_decode(json_encode((array) simplexml_load_string($xml)), 1); var_dump($array); ?> empty 将被转换为空数组而不是 null。
没有测试这个,但这似乎可以完成:
function convertXmlObjToArr($obj, &$arr)
{
$children = $obj->children();
foreach ($children as $elementName => $node)
{
$nextIdx = count($arr);
$arr[$nextIdx] = array();
$arr[$nextIdx]['@name'] = strtolower((string)$elementName);
$arr[$nextIdx]['@attributes'] = array();
$attributes = $node->attributes();
foreach ($attributes as $attributeName => $attributeValue)
{
$attribName = strtolower(trim((string)$attributeName));
$attribVal = trim((string)$attributeValue);
$arr[$nextIdx]['@attributes'][$attribName] = $attribVal;
}
$text = (string)$node;
$text = trim($text);
if (strlen($text) > 0)
{
$arr[$nextIdx]['@text'] = $text;
}
$arr[$nextIdx]['@children'] = array();
convertXmlObjToArr($node, $arr[$nextIdx]['@children']);
}
return;
}
【讨论】:
这是可能的。这是一个递归函数,它打印出父元素的标签以及没有更多子元素的标签+内容。你可以改变它来构建一个数组:
foreach( $simpleXmlObject as $element )
{
recurse( $element );
}
function recurse( $parent )
{
echo '<' . $parent->getName() . '>' . "\n";
foreach( $parent->children() as $child )
{
if( count( $child->children() ) > 0 )
{
recurse( $child );
}
else
{
echo'<' . $child->getName() . '>';
echo iconv( 'UTF-8', 'ISO-8859-1', $child );
echo '</' . $child->getName() . '>' . "\n";
}
}
echo'</' . $parent->getName() . '>' . "\n";
}
【讨论】:
我不明白这一点,因为 SimpleXMLObject 可以像数组一样受到威胁......
但如果你真的需要,只需在论坛中查看 chassagnette 在this thread 或this post 中的回答。
【讨论】:
取决于 CDATA、数组等的一些问题。 (见:SimpleXMLElement to PHP Array)
我认为,这将是最好的解决方案:
public function simpleXml2ArrayWithCDATASupport($xml)
{
$array = (array)$xml;
if (count($array) === 0) {
return (string)$xml;
}
foreach ($array as $key => $value) {
if (is_object($value) && strpos(get_class($value), 'SimpleXML') > -1) {
$array[$key] = $this->simpleXml2ArrayWithCDATASupport($value);
} else if (is_array($value)) {
$array[$key] = $this->simpleXml2ArrayWithCDATASupport($value);
} else {
continue;
}
}
return $array;
}
【讨论】:
这是我的 iterative(即使我认为您不会通过使用递归解析数据而导致堆栈爆炸)实现递归转换为数组。这是一种比通过 json_**decode 函数更直接的方式:
function xml2Array(SimpleXMLElement $el): stdClass {
$ret = $el;
$stack = [&$ret];
while (count($stack) > 0) {
$cur = &$stack[count($stack) - 1];
array_splice($stack, -1);
$cur = (object) (array) $cur;
foreach ($cur as $key => $child) {
$childRef = &$cur->{$key};
if ($child instanceof SimpleXMLElement)
$stack[count($stack) - 1] = &$childRef;
elseif(is_array($child))
foreach ($childRef as $ckey => $cell) {
if ($cell instanceof SimpleXMLElement)
$stack[count($stack) - 1] = &$childRef[$ckey];
}
}
}
return $ret;
}
【讨论】:
对于那些对 CDATA 案有疑虑的人,
将@ajayi-oluwaseun-emmanuel 的回答与this answer 结合起来对我有用:
$xml = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA);
$json = json_encode($xml);
$arr = json_decode($json,TRUE);
【讨论】: