Search code examples
phpxmlassociative-array

php simpleXMLElement to array: null value


I've got following XML:

<account>
    <id>123</id>
    <email></email>
    <status>ACTIVE</status>
</account>

I want to have it as an array variable. Therefore I read it with $xml = simplexml_load_file(). The simplest way to convert simpleXMLElement to an associative array I know is to grind it with: json_decode(json_encode((array) $xml),1);

The problem is that I don't want to get the email key as an empty array, but rather as a NULL value. As SimpleXMLElement, it looks like:

public 'email' => 
    object(SimpleXMLElement)[205]

whereas in array it looks like:

'email' => 
    array (size=0)
      empty

I'd like to get:

'email' => NULL

The only way to achieve this I thought of is iterate through all elements and replace empty array with null value. The problem is that my XML is way bigger (above is just to explain the problem) and I'd have to iterate a lot of XML elements (and this would be manual work - I'm looking for something automatic). Maybe I'm missing some options in one of the functions... or maybe there's another trick to do this?


Solution

  • I cannot add a comment, but I think this will work for you, it should be faster then a regex or a loop:

    //after you json_encode, before you decode
    $str = str_replace(':[]',':null',json_encode($array));
    

    An empty array in JSON is represented by "[]". Sometimes the arrays are parsed as objects, in that case (or as a fallback) you can replace ":{}" too.