如何从SimpleXMLElement对象中获取最后一个值?

时间:2017-06-01 11:32:20

标签: php xml

我尝试了很多,但我无法理解。我有这个:

SimpleXMLElement Object ( 
    [BrowseNodeId] => 10829411 
    [Name] => Römertöpfe 
    [Ancestors] => SimpleXMLElement Object 
            ( 
                [BrowseNode] => 
                    SimpleXMLElement Object 
                ( 
                    [BrowseNodeId] => 3311941 
                    [Name] => Töpfe & Pfannen 
                    [Ancestors] => SimpleXMLElement Object 
                        ( 
                            [BrowseNode] => SimpleXMLElement Object 
                            ( 
                                [BrowseNodeId] => 3094896031 
                                [Name] => Kochen 
                                [Ancestors] => SimpleXMLElement Object 
                                ( 
                                    [BrowseNode] => SimpleXMLElement Object 
                                    (
                                        [BrowseNodeId] => 3169011 
                                        [Name] => Kategorien 
                                        [IsCategoryRoot] => 1 
                                        [Ancestors] => SimpleXMLElement Object 
                                        ( 
                                            [BrowseNode] => SimpleXMLElement Object 
                                                ( 
                                                [BrowseNodeId] => 3167641 
                                                [Name] => Küche, Haushalt & Wohnen 
        ) ) ) ) ) ) ) ) )

我需要它的最后一个值(粗体):

  

[姓名] => Küche,Haushalt& WOHNEN

但我不知道,我怎么能读出最后一个值。有人有解决方案吗?

1 个答案:

答案 0 :(得分:0)

如果您想要使用密钥Name获取最后一个值,则可以这样做。

Try this code snippet here tried with example XML

$result="";
$array=json_decode(json_encode(simplexml_load_string($YOUR_XML_STRING)),true);
array_walk_recursive($array, function($value,$key) use (&$result){
    if($key=="Name")
    {
        $result=$value;
    }
});
print_r($result);