仅选择 PHP 中 xpath 结果集的第一项 [英] Selecting only the first item of an xpath result set in PHP
本文介绍了仅选择 PHP 中 xpath 结果集的第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前使用两个 PHP 语句实现了预期的结果:
I am currently achieving the desired outcome with two PHP statements:
$thisBlarg = $xmlResource->xpath('//blarg[@ID='.$someBlargID.']');
echo $thisBlarg[0]->name;
但是,不想满足于次佳,我真的希望这是一个声明,但 PHP 不喜欢这样:
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;
而且有充分的理由.但是我找不到强制 xpath 查询直接返回结果的方法.有什么建议吗?
And for good reason. But I can't find a way to force an xpath query to return the result directly. Any suggestions?
推荐答案
试试这个
echo current(($xmlResource->xpath('//blarg[@ID='.$someBlargID.']')))->name;
这篇关于仅选择 PHP 中 xpath 结果集的第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文