tags:

views:

52

answers:

1

Dear friends !

I'm really stucked and don't know how to implement an idea better. So, we have an XML file:

alt text

I've got an array by function dom_to_array()

function dom2array($node) {$result = array(); 
if($node->nodeType == XML_TEXT_NODE) { 
    $result = $node->nodeValue; 
} 
else { 
    if($node->hasAttributes()) { 
        $attributes = $node->attributes; 
        if(!is_null($attributes))  
            foreach ($attributes as $index=>$attr)  
                $result[$attr->name] = $attr->value; 
    } 
    if($node->hasChildNodes()){ 
        $children = $node->childNodes; 
        for($i=0;$i<$children->length;$i++) { 
            $child = $children->item($i); 
            if($child->nodeName != '#text') 
            if(!isset($result[$child->nodeName])) 
                $result[$child->nodeName] = $this->dom2array($child); 
            else { 
                $aux = $result[$child->nodeName]; 
                $result[$child->nodeName] = array( $aux ); 
                $result[$child->nodeName][] = $this->dom2array($child); 
            } 
        } 
    } 
} 
return $result; 

}

I've got an array with first XML element - STRUCTURE.

Array
--STRUCTURE
----PAGE
----PAGE
-- -- --PAGE
----PAGE

So the main question is how make array looks like this:

Array
----PAGE
----PAGE
-- -- --PAGE
----PAGE

How to do it better friends - i don't need to include "structure" into array, is it possible to create by DOM functions ?!

+2  A: 

Basically you just want to exclude the structure level from the created PHP array?

I guess, you do something like this right now:

$dom = new DOMDocument();
$dom->loadXML($xml);
$array = dom2array($dom);

The resulting array includes the structure key.

What you can do is start from the structure element like so:

...
$array = dom2array($dom->getElementsByTagName('structure')->item(0));

Or you leave it as is and do it this way using the array key:

$array = dom2array($dom);
$array = $array['structure']; // set $array to the subelements of "structure"
Max
Max, you the MAN ! Thank you very much !!PS Come to Russia, drink vodka !
Vovan
@Vovan Thank you, glad I could help :-) btw. Vodka is really my favorite drink ;)
Max