I have a problem with this function because typecasting every XML child to an array can be problematic when the text is between CDATA
tags.
I fixed this by checking if the result of the typecasting to an array is empty. If so typecast it to a string and you will get a proper result.
Here is my modified version with CDATA
support:
function SimpleXML2ArrayWithCDATASupport($xml)
{
$array = (array)$xml;
if (count($array) === 0) {
return (string)$xml;
}
foreach ($array as $key => $value) {
if (!is_object($value) || strpos(get_class($value), 'SimpleXML') === false) {
continue;
}
$array[$key] = SimpleXML2ArrayWithCDATASupport($value);
}
return $array;
}