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