将 SimpleXML 对象转换为数组 [英] Converting a SimpleXML Object to an Array

查看:32
本文介绍了将 SimpleXML 对象转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了将 SimpleXML 对象转换为数组的功能这里:

/*** function object2array - 将结果转换为数组的更简单方法*(需要 json 模块).** 该函数是 PHP 手册的一部分.** PHP 手册文本和注释由知识共享涵盖* Attribution 3.0 License, 版权所有 (c) PHP Documentation Group** @author Diego Araos,来自 klapmedia dot com 的 diego* @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>*/函数 object2array($object){返回 json_decode(json_encode($object), TRUE);}

所以我对 XML 字符串的采用是这样的:

function xmlstring2array($string){$xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);$array = json_decode(json_encode($xml), TRUE);返回 $array;}

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

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

感谢大家的帮助

解决方案

我在 PHP 手册注释中找到了这个

一个>:

/*** 函数 xml2array** 该函数是 PHP 手册的一部分.** PHP 手册文本和注释由知识共享涵盖* Attribution 3.0 License, 版权所有 (c) 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>*/函数 xml2array ( $xmlObject, $out = array () ){foreach ( (array) $xmlObject as $index => $node )$out[$index] = ( is_object ( $node ) ) ?xml2array ( $node ) : $node;返回 $out;}

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

I came across this function of converting a SimpleXML Object to an array here:

/**
 * 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:

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?

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.

Thanks all for any help

解决方案

I found this in the PHP manual comments:

/**
 * 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.

这篇关于将 SimpleXML 对象转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆