php 将 SimpleXML 对象转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6167279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a SimpleXML Object to an Array
提问by Abs
I came across this function of converting a SimpleXML Object to an array here:
我碰到SimpleXML对象转换为阵列的这种功能在这里:
/**
* 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);
}
So my adoption for an XML strings is like:
所以我对 XML 字符串的采用是这样的:
function xmlstring2array($string)
{
$xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);
$array = json_decode(json_encode($xml), TRUE);
return $array;
}
It works pretty well, but it seems a bit hacky? Is there a more efficient/robust way of doing this?
它工作得很好,但似乎有点hacky?有没有更有效/更强大的方法来做到这一点?
I know that the SimpleXML Object is close enough to an array because it makes use of the ArrayAccess interface in PHP but it still doesn't work great to use as an array with multi-dimensional arrays i.e. looping.
我知道 SimpleXML 对象与数组足够接近,因为它使用了 PHP 中的 ArrayAccess 接口,但它仍然不能很好地用作具有多维数组的数组,即循环。
Thanks all for any help
感谢大家的帮助
回答by Arjan
I found this in the PHP manual comments:
我在PHP 手册注释中发现了这一点:
/**
* function xml2array
*
* 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 k dot antczak at livedata dot pl
* @date 2011-04-22 06:08 UTC
* @link http://www.php.net/manual/en/ref.simplexml.php#103617
* @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 xml2array ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
return $out;
}
It could help you. However, if you convert XML to an array you will loose all attributes that might be present, so you cannot go back to XML and get the same XML.
它可以帮助你。但是,如果将 XML 转换为数组,则会丢失可能存在的所有属性,因此无法返回到 XML 并获得相同的 XML。
回答by Aleksandr Ryabov
Just (array)
is missing in your code before the simplexml object:
只是(array)
在 simplexml 对象之前的代码中缺少:
...
$xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);
$array = json_decode(json_encode((array)$xml), TRUE);
^^^^^^^
...