Selecting only the first item of an xpath result s

2019-01-26 17:32发布

I am currently achieving the desired outcome with two PHP statements:

$thisBlarg = $xmlResource->xpath('//blarg[@ID='.$someBlargID.']');
echo $thisBlarg[0]->name;

But, not wanting to settle for second best, I'd really prefer this to be one statement, but PHP doesn't like this:

echo $xmlResource->xpath('//blarg[@ID='.$someBlargID.']')[0]->name;

And for good reason. But I can't find a way to force an xpath query to return the result directly. Any suggestions?

1条回答
欢心
2楼-- · 2019-01-26 17:59

Try this

echo current(($xmlResource->xpath('//blarg[@ID='.$someBlargID.']')))->name;
查看更多
登录 后发表回答