-2

I've got SimpleXMLElement object of data.

To convert it to array I simply

$data = (array) $xmlObj;

And its ok, but data like <![CDATA[LOREM IPSUM]]> it converts to empty array. I need it to be converted to normal array row.

Code:

    $e = simplexml_load_string($fileContent);
    $books = array();
    foreach ($e->product as $book) {
        $books[] = (array) $book;
    }
Adam Pietrasiak
  • 12,773
  • 9
  • 78
  • 91

1 Answers1

1
function toArray(SimpleXMLElement $xml) {
        $array = (array)$xml;

        foreach ( array_slice($array, 0) as $key => $value ) {
            if ( $value instanceof SimpleXMLElement ) {
                $array[$key] = empty($value) ? NULL : toArray($value);
            }
        }
        return $array;
    }
Matheno
  • 4,112
  • 6
  • 36
  • 53