如何将SimpleXMLObject转换为PHP数组?

16

考虑以下代码:

$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->$tag正常工作?


1
请使用双引号而不是单引号(或根本不使用引号)。 - Pekka
3个回答

16

试试这个:

$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对象。 - dfsq
这种方法是可行的,虽然不是很好的技巧,但对于简单的小对象来说,使用函数等将对象转换为数组并没有意义。 - dfsq
顺便说一句,在使用simplexml_load_string之前不需要加上(array)。 - dfsq
我肯定更喜欢你的方法,我只是想展示一种快速简便的方式来完成它! - Book Of Zeus

15

Book Of Zeus 的代码被包装在函数中以便递归运行:

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
        )
    )
)

1
@dfsq - 这个好像不能在整个数组中工作,就好像它只返回了第一个条目...这可能吗? - Shackrock
@Shackrock 请查看《Zeus之书》中的解决方案。 - Hoa
@shackrock说得对,这对没有任何列表的SimpleXMLElements非常有用。出于我的目的,我只是删除了$arr[$tag] =三元行,并将其拆分成完整的条件,在$element是SimpleXMLElement的情况下,执行$arr[$tag][] = xml2array($element);。注意**[]**。不确定其他人是否需要这个,然而。 - notacouch

4
试试这个:
$array = json_decode(json_encode((array)$xml), TRUE);

1
欢迎来到 Stack Overflow!虽然这段代码可能回答了问题,但提供关于为什么和/或如何回答问题的额外上下文可以提高其长期价值。 - ryanyuyu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接