Jump to content
iwato

XML Nested Object to PHP Array

Recommended Posts

BACKGROUND:  I cannot find anything wrong with this function, but when I try to run it, PHP tells me that it is undefined.  Thus, I suspect that the problem has to do with my version number.  Where before I  was running with 5.6, I am now running with 5.3.

CODE

function xmlobj_to_array($obj) { 
    $namespace = $obj->getDocNamespaces(true); 
    $namespace[NULL] = NULL; 	
    $children = array(); 
    $attributes = array(); 
    $name = strtolower((string)$obj->getName()); 	
    $text = trim((string)$obj); 
    if( strlen($text) <= 0 ) { 
        $text = NULL; 
    } 	
    if(is_object($obj)) { 
        foreach( $namespace as $ns=>$nsUrl ) { 
            $objAttributes = $obj->attributes($ns, true); 
            foreach( $objAttributes as $attributeName => $attributeValue ) { 
                $attribName = strtolower(trim((string)$attributeName)); 
                $attribVal = trim((string)$attributeValue); 
                if (!empty($ns)) { 
                    $attribName = $ns . ':' . $attribName; 
                } 
                $attributes[$attribName] = $attribVal; 
            } 
            $objChildren = $obj->children($ns, true); 
            foreach( $objChildren as $childName=>$child ) { 
                $childName = strtolower((string)$childName); 
                if( !empty($ns) ) { 
                    $childName = $ns.':'.$childName; 
                } 
                $children[$childName][] = xmlObjToArr($child); 
            } 
        } 
    } 	
    return array( 
        'name'=>$name, 
        'text'=>$text, 
        'attributes'=>$attributes, 
        'children'=>$children 
    ); 
} 

QUESTION:  Please test the function and let me know whether it is my code or my version number.  If it is the code, please suggest the appropriate changes.

Roddy

Share this post


Link to post
Share on other sites

Never mind, I found the error.  I had changed the name of the function, but because it is recursive, I needed to change the name of the function within the function as well.

Hoorray! Hooray!

Roddy

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×