在PHP中从xpath返回的访问数组

时间:2014-05-08 12:25:53

标签: php xml xpath

如何访问(以显示)xpath返回的元素?

我的xpath调用是

$products = $this->_dom->xpath('//menu/category[@name="'.$category.'"]');

我得到的是

    Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => pizza ) [item] 
=> SimpleXMLElement Object ( [@attributes] => Array ( [name] => Tomato and Cheese ) [type] => 
Regular [available] => true [size] => Array ( [0] => SimpleXMLElement Object ( [@attributes] 
=> Array ( [name] => Small ) [price] => 5.50 ) [1] => SimpleXMLElement Object ( [@attributes] 
=> Array ( [name] => Large ) [price] => 9.75 ) ) ) ) [1] => SimpleXMLElement Object ( 
[@attributes] => Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( [@attributes] 
=> Array ( [name] => Pepperoni ) [type] => Regular [available] => true [size] => Array ( [0] 
=> SimpleXMLElement Object ( [@attributes] => Array ( [name] => Small ) [price] => 6.85 ) [1] 
=> SimpleXMLElement Object ( [@attributes] => Array ( [name] => Large ) [price] => 10.85 ) ) ) 
) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => pizza ) [item] => 
SimpleXMLElement Object ( [@attributes] => Array ( [name] => Meatball ) [type] => Regular 
[available] => true [size] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array 
( [name] => Small ) [price] => 6.85 ) [1] => SimpleXMLElement Object ( [@attributes] => Array 
( [name] => Large ) [price] => 10.85 ) ) ) ) [3] => SimpleXMLElement Object ( [@attributes] => 
Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [name] 
=> Hawaiian ) [type] => Regular [available] => true [size] => Array ( [0] => SimpleXMLElement 
Object ( [@attributes] => Array ( [name] => Small ) [price] => 7.95 ) [1] => SimpleXMLElement 
Object ( [@attributes] => Array ( [name] => Large ) [price] => 11.80 ) ) ) ) [4] => 
SimpleXMLElement Object ( [@attributes] => Array ( [name] => pizza ) [item] => 
SimpleXMLElement Object ( [@attributes] => Array ( [name] => Three Aces Special ) [type] => 
Speciality [available] => true [size] => Array ( [0] => SimpleXMLElement Object ( 
[@attributes] => Array ( [name] => Small ) [price] => 9.80 ) [1] => SimpleXMLElement Object ( 
[@attributes] => Array ( [name] => Large ) [price] => 15.80 ) ) ) ) [5] => SimpleXMLElement 
Object ( [@attributes] => Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( 
[@attributes] => Array ( [name] => Mediterranean ) [type] => Speciality [available] => true 
[size] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Small ) 
[price] => 9.80 ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Large ) 
[price] => 15.80 ) ) ) ) )

我想要做的是在一个列表中显示这些信息,所以我想知道如何访问SimpleXMLElement对象的元素。

由于

1 个答案:

答案 0 :(得分:1)

清理输出:

[0] => SimpleXMLElement Object ( 
    [@attributes] => Array ( [name] => pizza )
    [item] => SimpleXMLElement Object ( 
        [@attributes] => Array ( [name] => Tomato and Cheese ) 
        [type] => Regular 
        [available] => true 
        [size] => Array ( 
            [0] => SimpleXMLElement Object ( 
                [@attributes] => Array ([name] => Small ) 
                [price] => 5.50 ) 
            [1] => SimpleXMLElement Object (
                [@attributes] => Array ([name] => Large ) 
                [price] => 9.75 ) 
        ) 
    )
) 

这样的东西会起作用,虽然你的很多数据都被包装成属性,这会让它变得更加冗长,比如sizes属性。您可能需要投射物品才能获得价值。

$products = $this->_dom->xpath('//menu/category[@name="'.$category.'"]');

foreach ($products as $product) {
    // accessing SimpleXML values is tricky
    // $name is a SimpleXMLElement Object
    $name = $product['title']; 

    // $name2 has the string value of the title attribute
    $name2 = (string) $product['title']; 

    // following works because __toString is implemented
    // by SimpleXMLElement class  
    echo "Title: " . $product['title']; 

    // alternative syntax, unless you cast to a 
    // string, you have a SimpleXML Object  
    $name3 =  $product->attributes()->title;
}

请参阅Access @attributes data in SimpleXMLElement in PHP