2011-10-15 17 views
16

Si consideri il seguente codice:Come convertire SimpleXMLObject in array 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)', 
     ); 
    } 

uscita $detailsDetails array è la seguente:

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

che è sbagliato.

La mia domanda è, come fare il lavoro $element->$tag?

+1

utilizzare le virgolette doppie invece di virgolette singole (o nessuno) –

risposta

15

Book Of Zeus codice avvolto in funzione per farlo funzionare in modo ricorsivo:

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 buone pratiche Mi piace quello –

+1

@dfsq - questo sembra non funzionare attraverso l'intero array, è come se restituisse solo la prima voce per me ... è possibile? – Shackrock

+0

@Shackrock Guarda la soluzione di Book Of Zeus – Hoa

17

Prova questo:

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

Questo stamperà:

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 
       ) 

     ) 

) 

o se non ti piace questo, è possibile utilizzare una classe PHP come: http://www.bin-co.com/php/scripts/xml2array/

o vista dfsq risposta

+0

Non fa lavorare in modo ricorsivo. [storage_drives] => Oggetto SimpleXMLElement. – dfsq

+0

Sei corretto, hai bisogno di tutti loro come array? –

+0

@Gaurish Ho aggiornato la mia risposta –

3

provate questo:

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

Benvenuto in Stack Overflow! Sebbene questo codice possa rispondere alla domanda, fornire un contesto aggiuntivo sul perché e/o su come questo codice risponde alla domanda migliora il suo valore a lungo termine. – ryanyuyu

+0

Grazie per aver risparmiato un sacco di tempo .... –