Access @attributes data in SimpleXMLElement in PHP

前端 未结 2 1228
渐次进展
渐次进展 2020-12-14 07:49

Just wanted to start by saying I have read a LOT of the questions on this site about this exact problem, but I\'m still struggling to apply it to my scenari

相关标签:
2条回答
  • 2020-12-14 08:09

    Did you try:

    echo (string)$xml->product->attributes()->id;

    That should give you access to the attributes.

    If you have more than 1 product, it may be

    echo (string)$xml->product[0]->attributes()->id;

    You can also access the attributes using regular array notation such as:

    $xml->product['id']; // instead of $xml->product->attributes()->id
    

    See Example #5 from the SimpleXML Examples as well as the manual page on SimpleXMLElement::attributes() for more information.

    0 讨论(0)
  • 2020-12-14 08:14
    $array = (array)$obj;
    $prop_id = $array['@attributes'];
    
    0 讨论(0)
提交回复
热议问题