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