How to convert SimpleXMLObject into PHP Array?

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

Consider the following code:

$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)',         );     } 

Output $detailsDetails array is as follows:

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

which is wrong.

My question is, how to make $element->$tag work?

回答1:

Book Of Zeus code wrapped in function to make it work recursively:

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


回答2:

Try this:

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

This will output:

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

or if you don't like this, you can use a PHP class like: http://www.bin-co.com/php/scripts/xml2array/

or view dfsq answer



回答3:

Try This:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!