Php Xpath query returning “Array”

耗尽温柔 提交于 2019-12-08 11:55:53

问题


ive got my xml file and also heres my php script

 $db  =  simplexml_load_file("BIN/videos.xml");

$id = $_GET['id'];

$tq = "//video['@id=" . $id . "']/title[0]";
$dq = "//video['@id=" . $id . "']/description[1]";
$eq = "//video['@id=" . $id . "']/embed[2]";

$title  = $db->xpath($tq);

$description = $db->xpath($dq);

$embed  = $db->xpath($eq); 

include("design/lyt.php");

echo $embed . '<br>
<h1>' . $title . '</h1>
<p>' . $description . '</p>';

?>

Its supposed to display "Test" for them all! but it says "Array"


回答1:


Access the elements of the array returned from xpath...

PHP >= 5.4:

$title  = $db->xpath($tq)[0];

PHP < 5.4:

Update PHP :-)

or

list($title,)  = $db->xpath($tq);

or

$title  = $db->xpath($tq);
$title = $title[0];


来源:https://stackoverflow.com/questions/20061514/php-xpath-query-returning-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!