php - Show only the first object with SimpleXMLElement -
this question exact duplicate of:
after extracting data file in php xpath.
my code:
$xml = simplexml_load_file("ccv.xml"); foreach ($xml->xpath('./section[@label="education"]/section[@label="degrees"]') $details) { foreach ($details->field $f) { $a = $f->attributes(); if ('degree type'== $a['label']){ $x = $f->lov; var_dump($x); break; } } }
the result looks this
object(simplexmlelement)[10] public '@attributes' => array (size=1) 'id' => string '00000000000000000000000000000073' (length=32) public 0 => string 'doctorate' (length=9) object(simplexmlelement)[9] public '@attributes' => array (size=1) 'id' => string '00000000000000000000000000000072' (length=32) public 0 => string 'master's thesis' (length=15) object(simplexmlelement)[8] public '@attributes' => array (size=1) 'id' => string '00000000000000000000000000000084' (length=32) public 0 => string 'bachelor's equivalent' (length=21)
how can return first object doctorate.
in xpath can wrap initial query within ()[1]
xpath query return first match :
(xpath/query/that/may/return/multiple/result)[1]
for particular case should :
(./section[@label="education"]/section[@label="degrees"])[1]
Comments
Post a Comment