简体   繁体   中英

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,

I dont want to do this,

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

I want to go access someNameHere by its ID "hi"

Thanks a lot for your atention, ;D

You'll have to use XPath for that.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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