Simple SimpleXML to Array in PHP

Posted: 2013/01/25 in Development, PHP

All I want is to convert a super-simple SimpleXML object to an array.  “super-simple” means no attributes, etc.  Nothing that is XML-specific.

$xml_string = "
        <sub_first_thing_a>Hi, I am the content of the first thing.</sub_first_thing_a>
        <sub_first_thing_b>Hi, more content.</sub_first_thing_b>
    <second_thing>Notice, no attributes or anything fancy.</second_thing>
$simplexml_object = simplexml_load_string($xml_string);


Why?  Because a number of functions, libraries, etc. return SimpleXML objects.  For example, the 1.x version of the very-awesome AWS PHP SDK tends to return SimpleXML objects.  And 9 times out of 10 all I need is a multi-dimensional, usually keyed (associative) array.

Casting the SimpleXML object as an array ONLY works with the first-level of the SimpleXML object.

$as_array = (array) $simplexml_object;

// Result:
array(2) {
   object(SimpleXMLElement)#1 (2) {
      string(40) "Hi, I am the content of the first thing."
      string(17) "Hi, more content."
   string(40) "Notice, no attributes or anything fancy."

Why isn’t there a simplexml_as_array function?

Because XML has so many more great features than just named-nodes!  But, alas, I find myself often wanting just a ‘simple’ array…


There are a NUMBER of home-made functions available that parse SimpleXML into arrays.  Some are super compact while STILL capturing much of the nuance of XML.  While searching for a solution for the nth time this lifetime, I came across this link: Convert SimpleXML Object into PHP array on

On this page, author Eric Potvin does one of ‘those’ functions that traverses a SimpleXML object recursively.  I’ve written what amounts to the identical code a number of times myself.  But then he busts out this BEAUTY!

$desired_array = json_decode(json_encode((array) simplexml_load_string($xml_string)), 1);

I thought I remember reading that the json_encode/json_decode functions in PHP are pretty resource-intensive.  If that’s true, I’m sure this is not the more resource-friendly approach.  But it is SO exactly what I am wanting — in one line!!

HUGE thanks to Eric at!!

Update (2013 Jan 29):

A not-trivial part of my frustration with SimpleXML and why I just wanted an array came from an issue I didn’t know I had: CDATA.  Come to find out, there is an interesting caveat when using CDATA and SimpleXML which is, SimpleXML doesn’t do CDATA.  That’s why I was so frustrated… (ha).  There IS a pretty slick flag, though, that makes CDATA usable by SimpleXML wherein the CDATA is simply converted to a string.  Worked great in my presenting problem so now, per genius Ryan Paraman‘s push (see comments), I am staying with SimpleXML and not converting to an array.

The fix below continues to have $simplexml_object be an actual SimpleXML object (that’s the null part).  But now CDATA is simply cast as a string with the LIBXML_NOCDATA flag:

$simplexml_object = simplexml_load_string($xml_string, null, LIBXML_NOCDATA);

Hurray for people who think ahead! (I’m looking at you, PHP Internals folks!!)

  1. Ryan Parman says:

    SimpleXML syntax is nearly identical to an associative array. Instead of [‘node’][‘subnode’], you use node->subnode. But you also have to typecast it. Not a big deal.

    The bigger feature of SimpleXML that Arrays don’t give you is the ability to use XPath to query data very, very, very quickly.

    But yes, this is a very fast way to convert a SimpleXML object into an associative array. I would just argue that you’d be far better off actually learning how to use the very powerful SimpleXML extension.

  2. Aye, Ryan, you are correct. Honestly, the reason I wanted some way to go from SimpleXML to simple array (sic) is because I know arrays and I don’t really now SimpleXML (per your last sentence).

    Because I use XML so seldom, the benefit to me is that I can use what I know and use it swiftly. The value of learning SimpleXML hasn’t, to date, matched the cost of learning SimpleXML. But as I pondered this very response… I thought, maybe now is the time.

    So now on my short-list is: “learn beginner and intermediate SimpleXML syntax”. Consider me schooled, Ryan (grin).

  3. Maddigger says:

    I have program doing this, but there will be issues with json conversion if you try to convert larger xml file like this to array.. takes really lot of memory.. i have 512mb php memory limit.. still not enough.

    • Good catch, Maddigger! Another reason why this approach, while perhaps fine for quick, ‘utility-like’ needs… this is NOT a great solution. More reason for me to get better at SimpleXML!

Leave a Reply

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

You are commenting using your 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