Php Xpath query returning “Array”

2019-06-11 15:25发布

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条回答
2楼-- · 2019-06-11 16:02

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];
查看更多
登录 后发表回答