Pregunta

Necesito emitir recursivamente un PHP SimpleXMLObject a una matriz. El problema es que cada subelemento también es un elemento PHP SimpleXMLE.

¿Es esto posible?

¿Fue útil?

Solución

json_decode(json_encode((array) simplexml_load_string($obj)), 1);

Otros consejos

No probé este, pero parece que se hace:

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

Tomado de http://www.codingforums.com/showthread.php?t= 87283

Es posible. Esta es una función recursiva que imprime las etiquetas de elementos principales y las etiquetas + contenido de elementos que no tienen más elementos secundarios. Puede modificarlo para crear una matriz:

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

No veo el punto ya que SimpleXMLObject puede ser tratado igual que las matrices de todos modos ...

Pero si realmente necesita eso, simplemente verifique la respuesta de chassagnette en este hilo o esta publicación en un foro.

Dependiendo de algunos problemas con CDATA, matrices, etc. (ver: SimpleXMLElement to PHP Array )

Creo que esta sería la mejor solución:

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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top