“At sign” @ in SimpleXML object?

前端 未结 4 2004
臣服心动
臣服心动 2020-12-04 01:56

This is the output of print_r() run on a typical SimpleXMLElement object:

SimpleXMLElement Object
(
    [@attributes] => Array
        (

            


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 02:14

    Sorry, can't comment as a guest but for anyone else who ends up here like I did... I am creating my own Joomla form fields and Joomla creates a very 'interesting' object of all sorts of things. Now, I didn't want to become a SimpleXML expert, all I wanted was the original label text which was squirrelled away in @attributes.

    After a bit of "hmmm, I wonder if this works?"™ I found this is the easiest way of accessing these values:

    var_dump($simpleXMLObject);
    
    /* Result */
    object(SimpleXMLElement)
      public '@attributes' => 
        array (size=3)
          'name' => string 'awesome'
          'label' => string 'Awesome Label'
          'type' => string 'typeOfAwesome'
    
    echo $simpleXMLObject->attributes()->label; // Awesome Label
    
    $simpleXMLObject->attributes()->label = 'Different Day, Different Awesome';
    echo $simpleXMLObject->attributes()->label; // Different Day, Different Awesome 
    

    They were not lying. It really is simple.

提交回复
热议问题