开发者

PHP DOM array - cut the first element - better way to do it

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

PHP DOM array - cut the first element - better way to do it

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); 
            } 
    开发者_如何学Go    } 
    } 
} 
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 ?!


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜