2011-10-15 114 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

謝謝它節省了我很多時間.... –