title: Access @attributes data in SimpleXMLElement in PHP
body: <p>Just wanted to start by saying I <strong>have</strong> read a LOT of the questions on this site about this exact problem, but I'm still struggling to apply it to my scenario. If someone could help me out, that'd be great! :)</p>
  
  <p>I am trying to extract data from the following XML:</p>
  
  <pre><code>$myXML = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;products&gt;&lt;product uri="https://192.168.110.201:9630/api/products/1807/" id="1807"    resource_type="current"&gt;&lt;code&gt;DEMO - MC700X/A&lt;/code&gt;&lt;flags&gt;&lt;inventoried&gt;true&lt;/inventoried&gt;&lt;editable_sell&gt;false&lt;/editable_sell&gt;&lt;master_model&gt;false&lt;/master_model&gt;&lt;/flags&gt;&lt;sell_price&gt;0.00&lt;/sell_price&gt;&lt;description&gt;Apple MC700X/A Demo&lt;/description&gt;&lt;inventory&gt;&lt;available&gt;7&lt;/available&gt;&lt;reserved&gt;0&lt;/reserved&gt;&lt;coming_for_stock&gt;2.0&lt;/coming_for_stock&gt;&lt;coming_for_customer&gt;0.0&lt;/coming_for_customer&gt;&lt;warehouses&gt;0&lt;/warehouses&gt;&lt;total&gt;7&lt;/total&gt;&lt;/inventory&gt;&lt;product_photos/&gt;&lt;/product&gt;&lt;/products&gt;';
  </code></pre>
  
  <p>I am using SimpleXML to put it into a PHP variable (object?) like this:</p>
  
  <pre><code>$xml = new SimpleXMLElement($myXML);
  </code></pre>
  
  <p>If I do a:</p>
  
  <pre><code>echo '&lt;pre&gt;';
  print_r($xml);
  echo '&lt;/pre&gt;';
  </code></pre>
  
  <p>I get the following returned:</p>
  
  <pre><code>SimpleXMLElement Object
  (
      [product] =&gt; SimpleXMLElement Object
          (
              [@attributes] =&gt; Array
                  (
                      [uri] =&gt; https://192.168.110.201:9630/api/products/1807/
                      [id] =&gt; 1807
                      [resource_type] =&gt; current
                  )
  
              [code] =&gt; DEMO - MC700X/A
              [flags] =&gt; SimpleXMLElement Object
                  (
                      [inventoried] =&gt; true
                      [editable_sell] =&gt; false
                      [master_model] =&gt; false
                  )
  
              [sell_price] =&gt; 0.00
              [description] =&gt; Apple MC700X/A Demo
              [inventory] =&gt; SimpleXMLElement Object
                  (
                      [available] =&gt; 7
                      [reserved] =&gt; 0
                      [coming_for_stock] =&gt; 2.0
                      [coming_for_customer] =&gt; 0.0
                      [warehouses] =&gt; 0
                      [total] =&gt; 7
                  )
  
              [product_photos] =&gt; SimpleXMLElement Object
                  (
                  )
  
          )
  
  )
  </code></pre>
  
  <p>Now, when I try to access that data programatically, the following works fine:</p>
  
  <pre><code>// This returns the value as expected
  echo '&lt;pre&gt;';
  echo($xml-&gt;product-&gt;code);
  echo '&lt;br&gt;';
  echo($xml-&gt;product-&gt;sell_price);
  echo '&lt;br&gt;';
  echo($xml-&gt;product-&gt;inventory-&gt;available);
  echo '&lt;br&gt;';
  echo '&lt;/pre&gt;';
  </code></pre>
  
  <p>This returns:</p>
  
  <pre><code>DEMO - MC700X/A
  0.00
  7
  </code></pre>
  
  <p>But I need to be able to access the "id" tag in the base "product" element (i.e. the @attributes bit), but can't work it out. I've been reading a lot, and figured that I should be able to use the attributes() method, but I can't work it out exactly.</p>
  
  <p>Trying this didn't work:</p>
  
  <pre><code>echo '&lt;pre&gt;';
  echo($xml-&gt;attributes());
  echo '&lt;br&gt;';
  echo '&lt;/pre&gt;';
  </code></pre>
  
  <p>It just returns nothing. Can someone please help me? I want to be able to display the "id" tag.. i.e. what I would expect to be:</p>
  
  <pre><code>echo $xml['product']['@attributes']['id'];
  </code></pre>
  
  <p>obviously doesn't work either.</p>
  
  <p>Thanks!!
  John</p>