title: How to convert SimpleXMLObject into PHP Array?
body: <p>Consider the following code:</p>
  
  <pre><code>$string = '&lt;device&gt;
      &lt;id&gt;1234&lt;/id&gt;
      &lt;label&gt;118&lt;/label&gt;
      &lt;username&gt;root&lt;/username&gt;
      &lt;password&gt;helloWorld&lt;/password&gt;
      &lt;hardware&gt;
          &lt;memory&gt;4GB RAM&lt;/memory&gt;
          &lt;storage_drives&gt;
              &lt;storage_drive_1&gt;2TB SATA 7,200RPM&lt;/storage_drive_1&gt;
              &lt;storage_drive_2&gt;1TB SATA 7,200RPM&lt;/storage_drive_2&gt;
              &lt;storage_drive_3&gt;Not Applicable&lt;/storage_drive_3&gt;
              &lt;storage_drive_4&gt;Not Applicable&lt;/storage_drive_4&gt;
          &lt;/storage_drives&gt;
      &lt;/hardware&gt;
  &lt;/device&gt;';
  $xml = new SimpleXMLElement($string);
  
  $deviceDetails = Array();
  foreach($xml as $element){
      $tag = $element-&gt;getName();
      $deviceDetails +=  Array($tag =&gt; '$element-&gt;$tag)',
          );
      }
  </code></pre>
  
  <p>Output <code>$detailsDetails</code> array is as follows:</p>
  
  <pre><code>Array
  (
      [id] =&gt; $element-&gt;$tag)
      [label] =&gt; $element-&gt;$tag)
      [username] =&gt; $element-&gt;$tag)
      [password] =&gt; $element-&gt;$tag)
      [hardware] =&gt; $element-&gt;$tag)
  )
  </code></pre>
  
  <p>which is wrong.</p>
  
  <p>My question is, <strong>how to make <code>$element-&gt;$tag</code> work?</strong></p>