Articles

Convert XML Object to Array in PHP

In PHP on October 15, 2011 by patrellery


Here’s a very simple way to convert an XML Object to an array in PHP.

$string = '
<user>
  <id>1234</id>
  <username>root</username>
  <password>helloWorld</password>
  <access>
    <admin>0</admin>
    <write>1</write>
    <read>1</read>
  </access>
  <messages>
    <message>Message 1</message>
    <message>Message 1</message>
  </messages>
</user>';

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

easy no?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: