简体   繁体   English

PHP,SimpleXML,如何访问<tag>通过其 id 属性而不是数组位置?

[英]PHP, SimpleXML, how to access <tag> by its id attribue and not array position?

I would like to understand how to access a by its id and not trough array position, example,我想了解如何通过其 id 而不是通过数组位置访问 a,例如,

I dont want to do this,我不想这样做,

$myXML->someNameHere[1]->home[0] $myXML->someNameHere[1]->home[0]

I want to go access someNameHere by its ID "hi"我想通过它的 ID "hi" 访问 someNameHere

Thanks a lot for your atention, ;D非常感谢您的关注,;D

You'll have to use XPath for that.为此,您必须使用XPath

$nodes = $myXML->xpath('//*[@id="hi"]');

if (!empty($nodes))
{
    $someNameHere = $nodes[0];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM