dqx36753
2014-03-08 15:04
浏览 82

php xpath将节点值作为Array返回

Im using xpath to get values for a cms page,

video?id=blahblah

My PHP Xpath Code is

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

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

<data>
<video id="1">
<title>Test</title>
</video>

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

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

我使用xpath获取cms页面的值,

  video?id = blahblah 
   
 
 

我的PHP Xpath代码是

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

我为所有值执行此操作,下面是我的xml代码

 &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];
    
    打赏 评论

相关推荐 更多相似问题