将SimpleXML对象转换为数组

Abs*_*Abs 66 php xml arrays

我碰到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); 
}
Run Code Online (Sandbox Code Playgroud)

所以我对XML字符串的采用就像:

function xmlstring2array($string)
{
    $xml   = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);

    $array = json_decode(json_encode($xml), TRUE);

    return $array;
}
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但似乎有点hacky?有没有更有效/更强大的方法来做到这一点?

我知道SimpleXML对象足够接近数组,因为它在PHP中使用了ArrayAccess接口,但它仍然不能很好地用作具有多维数组的数组,即循环.

谢谢大家的帮助

Arj*_*jan 84

我在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;
}
Run Code Online (Sandbox Code Playgroud)

它可以帮助你.但是,如果将XML转换为数组,则会丢失可能存在的所有属性,因此您无法返回XML并获取相同的XML.

  • 请注意,这不会将多维对象转换为纯数组,即子元素仍然是`SimpleXMLElement`s.这是因为`is_object($ node)`为子对象返回`false`,即第二次递归中的`gettype($ node)`是""array"`.更合适的方法是[正下方的评论](http://www.php.net/manual/en/ref.simplexml.php#111227).第二种方法与第一种(原始)方法和json方法相比具有一些性能影响:http://codepad.viper-7.com/eHhSNR,这使得json方法再次可用于性能方面 (14认同)
  • @Arjan:您错过了对版权所有者和作者的正确归属。出于方便的原因,我还添加了一些许可 docblock 标签。当您从网站上的 PHP 手册复制代码示例时请小心。它们与许可证兼容,但它们需要归属。由于 CC 不是专门为软件设计的,因此代码示例很可能无法在现实生活中使用,它们仅可用于展示/解释某些内容。如果您不归属和隐藏版权所有者,这通常会丢失。 (2认同)
  • 这个函数不起作用递归.这是一个更好的版本:http://stackoverflow.com/questions/2726487/simplexmlelement-to-php-array/24919807#24919807 (2认同)

Ale*_*bov 58

只是(array)缺少在simplexml的对象之前您的代码:

...

$xml   = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);

$array = json_decode(json_encode((array)$xml), TRUE);
                                 ^^^^^^^
...
Run Code Online (Sandbox Code Playgroud)

  • @MonkeyMonk,只有XML具有"扁平"结构才是真的.警告!使用`json _**`获取数组会产生不同的结果,具体取决于子对象的数量(一对多). (9认同)
  • 您不必使用`json _**code`.只需使用`(object)(array)$ xml`即可完成!;-) (4认同)
  • 这种方法缺少CDATA内容.用PHP 5.5.21测试 (3认同)