来自PHP简单XML的值

I getting the following XML from an API and would like to use PHP to display it to a website.

<entityset>
<entity id="30751592" name="RFC-123456: name of change">
<template id="101" name="Request for Change" code="request_for_change"/>
<group id="62" name="Changes"/>
<attribute id="1108" name="Change requester" code="change_requester">
<reference id="19627173" name="Test name"/>
</attribute>
<attribute id="1109" name="Subject" code="subject">
<value>subject name</value>
</attribute><attribute id="1110" name="Description" code="description">
<value>Some description text</value>
</attribute>
<attribute id="1111" name="Status" code="status">
<value>2 - Approved</value>
</attribute>
<attribute id="1112" name="Change category" code="change_category">
<value>2 - Major</value>
</attribute>
</entity>
</entityset>

As you may notice, the format is a bit different depending on the data returned. Some will be returned as "value", some as a reference.

Data($data) is coming from a http query (CURL). That part is working. This is that I have tried so far:

$xml=simplexml_load_string($data);

$attributes = $xml->entity[0]->attribute;

print $attributes[0]; 
print $attributes[1]; 
print $attributes[2]; 
print $attributes[3];

How to fetch this as a correct array(associative is possible, but not a req.)?