douping7975 2013-04-02 17:29
浏览 51
已采纳

如何访问具有@符号的PHP对象属性? [重复]

This question already has an answer here:

My problem is similar to this one

How do I access a PHP object attribute having a dollar sign?

but I have an @ (at) sign instead of a dollar.

The object is this:

object(SimpleXMLElement)#8 (1) { 
  ["@attributes"]=> array(3) { 
  ["type"]=> string(9) "image/png" 
  ["href"]=> string(62) "http://someurl.com/images/193/image_normal.jpg" 
  ["rel"]=> string(5) "image" 
  } 
}

and I have to access the @attributes variable (the href component, really), but PHP doesn't allow such a syntax:

$object->@attributes

Following the cited resource, I tried either this way:

$object->{'@attributes'};

or

$myvar = '@attributes';
$object->$myvar;

but neither of the two forms leads to access the variable. It prints:

object(SimpleXMLElement)#8 (0) {}

while I would expect a vector.

Any idea? thanks

</div>
  • 写回答

2条回答 默认 最新

  • doue8385 2013-04-02 17:34
    关注

    Its is attributes see SimpleXMLElement::attributes for more information

    Example

    foreach($xml->attributes() as $a => $b) {
        echo $a,'="',$b,"\"
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图