When converting xml to php array, empty xml fields creating empty array, not empty string

I am using a nice short piece of code to turn my XML string into a php array

$products = json_decode(json_encode(simplexml_load_string($products_xml)),TRUE);

      

Everything works fine except when the xml field is empty, when I get an empty array and not a string - and when it writes to my SQL database, I see the string "Array" and not an empty field.

Is there a good way to do this using php?


In the meantime, I managed to solve the problem with this code:

$products = json_decode(str_replace('{}', '""', json_encode(simplexml_load_string($products_xml))),TRUE);

      

+3


source to share


1 answer


If any content field of your json_encoded xml contains "{}", this code will be broken. This happened to me, I am working on a solution to prevent it.



0


source







All Articles