protected static function _toArray($xml, &$parentData, $ns, $namespaces)
    {
        $data = [];

        foreach ($namespaces as $namespace) {
            foreach ($xml->attributes($namespace, true) as $key => $value) {
                if (!empty($namespace)) {
                    $key = $namespace . ':' . $key;
                }
                $data['@' . $key] = (string)$value;
            }

            foreach ($xml->children($namespace, true) as $child) {
                static::_toArray($child, $data, $namespace, $namespaces);
            }
        }

        $asString = trim((string)$xml);
        if (empty($data)) {
            $data = $asString;
        } elseif (strlen($asString) > 0) {
            $data['@'] = $asString;
        }

        if (!empty($ns)) {
            $ns .= ':';
        }
        $name = $ns . $xml->getName();
        if (isset($parentData[$name])) {
            if (!is_array($parentData[$name]) || !isset($parentData[$name][0])) {
                $parentData[$name] = [$parentData[$name]];
            }
            $parentData[$name][] = $data;
        } else {
            $parentData[$name] = $data;
        }
    }
protected static function _toArray($xml, &$parentData, $ns, $namespaces)
    {
        $data = [];

        foreach ($namespaces as $namespace) {
            foreach ($xml->attributes($namespace, true) as $key => $value) {
                if (!empty($namespace)) {
                    $key = $namespace . ':' . $key;
                }
                $data['@' . $key] = (string)$value;
            }

            foreach ($xml->children($namespace, true) as $child) {
                static::_toArray($child, $data, $namespace, $namespaces);
            }
        }

        $asString = trim((string)$xml);
        if (empty($data)) {
            $data = $asString;
        } elseif (strlen($asString) > 0) {
            $data['@'] = $asString;
        }

        if (!empty($ns)) {
            $ns .= ':';
        }
        $name = $ns . $xml->getName();
        if (isset($parentData[$name])) {
            if (!is_array($parentData[$name]) || !isset($parentData[$name][0])) {
                $parentData[$name] = [$parentData[$name]];
            }
            $parentData[$name][] = $data;
        } else {
            $parentData[$name] = $data;
        }
    }
protected static function _toArray ( $ xml , & $ parentData , $ ns , $ namespaces ) { $ data = [ ] ; foreach ( $ namespaces as $ namespace ) { foreach ( $ xml -> attributes ( $ namespace , true ) as $ key => $ value ) { if ( ! empty ( $ namespace ) ) { $ key = $ namespace . ':' . $ key ; } $ data [ '@' . $ key ] = ( string ) $ value ; } foreach ( $ xml -> children ( $ namespace , true ) as $ child ) { static :: _toArray ( $ child , $ data , $ namespace , $ namespaces ) ; } } $ asString = trim ( ( string ) $ xml ) ; if ( empty ( $ data ) ) { $ data = $ asString ; } elseif ( strlen ( $ asString ) > 0 ) { $ data [ '@' ] = $ asString ; } if ( ! empty ( $ ns ) ) { $ ns .= ':' ; } $ name = $ ns . $ xml -> getName ( ) ; if ( isset ( $ parentData [ $ name ] ) ) { if ( ! is_array ( $ parentData [ $ name ] ) || ! isset ( $ parentData [ $ name ] [ 0 ] ) ) { $ parentData [ $ name ] = [ $ parentData [ $ name ] ] ; } $ parentData [ $ name ] [ ] = $ data ; } else { $ parentData [ $ name ] = $ data ; } }
Recursive method to toArray

@param \SimpleXMLElement $xml SimpleXMLElement object
@param array $parentData Parent array with data
@param string $ns Namespace of current child
@param array $namespaces List of namespaces in XML
@return void
Recursive method to toArray