2011-10-15 145 views
16

考虑下面的代码:如何将SimpleXMLObject转换为PHP数组?

$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

使用双引号,而不是单引号(或根本没有) –

回答

15

书的宙斯代码包在功能,使之递归调用

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

+1好习惯我喜欢这个 –

+1

@dfsq - 这似乎不适用于整个数组,这就像它只会返回我的第一个条目...是可能的吗? – Shackrock

+0

@Shackrock查看Book Of Zeus的解决方案 – Hoa

17

试试这个:

$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 answer

+0

它不递归地工作。 [storage_drives] => SimpleXMLElement对象。 – dfsq

+0

你是对的,你需要把它们全部当作数组? –

+0

@Gaurish我更新了我的回答 –

3

试试这个:

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

欢迎来到堆栈溢出!虽然这段代码可能会回答这个问题,但为什么和/或代码如何回答这个问题提供了额外的背景,这提高了它的长期价值。 – ryanyuyu

+0

谢谢它节省了我很多时间.... –