开发者

Selecting only the first item of an xpath result set in PHP

开发者 https://www.devze.com 2022-12-29 14:30 出处:网络
I am currently achieving the desired outcome with two PHP stat开发者_开发问答ements: $thisBlarg = $xmlResource->xpath(\'//blarg[@ID=\'.$someBlargID.\']\');

I am currently achieving the desired outcome with two PHP stat开发者_开发问答ements:

$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?


Try this

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号