Convertendo um objeto SimpleXML em uma matriz [fechado]
Deparei-me com essa função de converter um objeto SimpleXML em uma matrizAqu:
/**
* function object2array - A simpler way to transform the result into an array
* (requires json module).
*
* This function is part of the PHP manual.
*
* The PHP manual text and comments are covered by the Creative Commons
* Attribution 3.0 License, copyright (c) the PHP Documentation Group
*
* @author Diego Araos, diego at klapmedia dot com
* @date 2011-02-05 04:57 UTC
* @link http://www.php.net/manual/en/function.simplexml-load-string.php#102277
* @license http://www.php.net/license/index.php#doc-lic
* @license http://creativecommons.org/licenses/by/3.0/
* @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0>
*/
function object2array($object)
{
return json_decode(json_encode($object), TRUE);
}
Então, minha adoção para cadeias XML é como:
function xmlstring2array($string)
{
$xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);
$array = json_decode(json_encode($xml), TRUE);
return $array;
}
Funciona muito bem, mas parece um pouco hacky? Existe uma maneira mais eficiente / robusta de fazer isso?
ei que o SimpleXML Object está próximo o suficiente de uma matriz porque faz uso da interface ArrayAccess no PHP, mas ainda não funciona muito bem como matriz com matrizes multidimensionais, por exemplo, loo
Obrigado a todos por qualquer ajuda