【问题标题】:How to convert SimpleXMLObject into PHP Array?如何将 SimpleXMLObject 转换为 PHP 数组?
【发布时间】:2011-10-15 15:41:14
【问题描述】:

考虑以下代码:

$string = '<device>
    <id>1234</id>
    <label>118</label>
    <username>root</username>
    <password>helloWorld</password>
    <hardware>
        <memory>4GB RAM</memory>
        <storage_drives>
            <storage_drive_1>2TB SATA 7,200RPM</storage_drive_1>
            <storage_drive_2>1TB SATA 7,200RPM</storage_drive_2>
            <storage_drive_3>Not Applicable</storage_drive_3>
            <storage_drive_4>Not Applicable</storage_drive_4>
        </storage_drives>
    </hardware>
</device>';
$xml = new SimpleXMLElement($string);

$deviceDetails = Array();
foreach($xml as $element){
    $tag = $element->getName();
    $deviceDetails +=  Array($tag => '$element->$tag)',
        );
    }

输出$detailsDetails数组如下:

Array
(
    [id] => $element->$tag)
    [label] => $element->$tag)
    [username] => $element->$tag)
    [password] => $element->$tag)
    [hardware] => $element->$tag)
)

这是错误的。

我的问题是,如何让$element-&gt;$tag 工作?

【问题讨论】:

  • 使用双引号代替单引号(或根本不使用)

标签: php arrays simplexml


【解决方案1】:

试试这个:

$string = '<device>
  <id>1234</id>
  <label>118</label>
  <username>root</username>
  <password>helloWorld</password>
  <hardware>
    <memory>4GB RAM</memory>
     <storage_drives>
      <storage_drive_1>2TB SATA 7,200RPM</storage_drive_1>
      <storage_drive_2>1TB SATA 7,200RPM</storage_drive_2>
      <storage_drive_3>Not Applicable</storage_drive_3>
      <storage_drive_4>Not Applicable</storage_drive_4>
    </storage_drives>
  </hardware>
</device>';
  
$xml = json_decode(json_encode((array) simplexml_load_string($string)), 1);

这将输出:

Array
(
    [id] => 1234
    [label] => 118
    [username] => root
    [password] => helloWorld
    [hardware] => Array
        (
            [memory] => 4GB RAM
            [storage_drives] => Array
                (
                    [storage_drive_1] => 2TB SATA 7,200RPM
                    [storage_drive_2] => 1TB SATA 7,200RPM
                    [storage_drive_3] => Not Applicable
                    [storage_drive_4] => Not Applicable
                )

        )

)

或者如果你不喜欢这样,你可以使用一个 PHP 类,比如:http://www.bin-co.com/php/scripts/xml2array/

或查看dfsq答案

【讨论】:

  • 它不能递归工作。 [storage_drives] => SimpleXMLElement 对象。
  • 这个方法+1。虽然这样做并不是很好的技术,但对于简单的小对象,使用函数等将对象转换为数组是没有意义的。
  • 顺便说一句,simplexml_load_string 之前不需要(数组)
  • 我肯定更喜欢你的方法,我只是想展示一个快速简单的方法!
【解决方案2】:

宙斯之书代码包裹在函数中,使其递归工作:

function xml2array($xml)
{
    $arr = array();

    foreach ($xml as $element)
    {
        $tag = $element->getName();
        $e = get_object_vars($element);
        if (!empty($e))
        {
            $arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e;
        }
        else
        {
            $arr[$tag] = trim($element);
        }
    }

    return $arr;
}

$xml = new SimpleXMLElement($string);
print_r(xml2array($xml));

Array
(
    [id] => 1234
    [label] => 118
    [username] => root
    [password] => helloWorld
    [hardware] => Array
    (
        [memory] => 4GB RAM
        [storage_drives] => Array
        (
            [storage_drive_1] => 2TB SATA 7,200RPM
            [storage_drive_2] => 1TB SATA 7,200RPM
            [storage_drive_3] => Not Applicable
            [storage_drive_4] => Not Applicable
        )
    )
)

【讨论】:

  • @dfsq - 这似乎不适用于整个数组,就像它只为我返回第一个条目......这可能吗?
  • @Shackrock 查看宙斯之书的解决方案
  • @shackrock 是对的,这对于没有任何列表的 SimpleXMLElements 非常有用。出于我自己的目的,我刚刚删除了$arr[$tag] = 三元行并将其拆分为完整条件,如果$element 是一个SimpleXMLElement,请执行$arr[$tag][] = xml2array($element);。注意 []。不过,不确定这是否是其他人需要的。
【解决方案3】:

试试这个:

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

【讨论】:

  • 欢迎来到 Stack Overflow!虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
猜你喜欢
  • 2010-12-07
  • 2017-03-29
  • 1970-01-01
  • 2014-11-25
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
相关资源
最近更新 更多