Php Xpath query returning “Array”

2019-06-11 15:21发布

问题:

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];