26

我需要递归地将 PHP SimpleXMLObject 转换为数组。问题是每个子元素也是一个 PHP SimpleXMLElement。

这可能吗?

4

7 回答 7

76
json_decode(json_encode((array) simplexml_load_string($obj)), 1);
于 2012-12-18T23:11:46.263 回答
6

没有测试这个,但这似乎可以完成:

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; 
} 

取自http://www.codingforums.com/showthread.php?t=87283

于 2009-05-07T14:19:39.200 回答
0

有可能的。这是一个递归函数,它打印出父元素的标签以及没有更多子元素的标签+内容。你可以改变它来构建一个数组:

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";
}
于 2009-05-07T14:23:20.127 回答
0

我不明白这一点,因为 SimpleXMLObject 可以像数组一样受到威胁......

但是,如果您真的需要,只需查看 chassagnette 在此线程中的答案或论坛中的此帖子即可。

于 2009-05-07T14:24:23.417 回答
0

取决于 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;
}
于 2019-02-27T18:04:25.177 回答
0

在这里,我的迭代(即使我认为您不会通过使用递归解析数据来获得堆栈爆炸)实现递归转换为数组。这是一种比通过 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;
}
于 2020-03-04T11:32:18.703 回答
0

对于那些对 CDATA 案有疑虑的人,

将@ajayi-oluwaseun-emmanuel 的答案与这个答案结合起来对我有用:

$xml = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA);
$json = json_encode($xml);
$arr = json_decode($json,TRUE);
于 2021-03-17T09:26:57.613 回答