2014-03-08 15:04
浏览 82

php xpath将节点值作为Array返回

Im using xpath to get values for a cms page,


My PHP Xpath Code is

$db = simplexml_load_file("data.xml");
$title = $db->xpath("//video[@id=$id]/title");

Im doing this for all values, below is my xml code

<video id="1">

I want the title heading to say Test but it says array instead

图片转代码服务由CSDN问答提供 功能建议


  video?id = blahblah 

我的PHP Xpath代码是

 $ db  = simplexml_load_file(“data.xml”); 
 $ title = $ db-&gt; xpath(“// video [@ id = $ id] / title”); 


 &lt; data&gt; 
&lt; video id =“1”&gt; 
&lt; title&gt  ;测试&lt; / title&gt; 
&lt; / video&gt; 


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duandu2980 2014-03-08 18:12

    XPath queries return a sequence of result nodes, not a single one. SimpleXML returns this as an array of SimpleXMLElements (which have reasonable __toString implementations, so you can directly output them in string context).

    So if you're sure you will only have a single result, just pick the first item of the array:

    $title = $db->xpath("//video[@id=$id]/title")[0];
    打赏 评论

相关推荐 更多相似问题