0

I've retrieved data from an XML file like so

$response = simplexml_load_file($url);

print_r displays the following

SimpleXMLElement Object
(
[artist] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [type] => Group
                [id] => b9fb5447-7f95-4a6a-a157-afed2d7b9f4c
            )

        [name] => He Is Legend
        [sort-name] => He Is Legend
        [country] => US
        [area] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [id] => 489ce91b-6658-3307-9877-795b68554c98
                    )

                [name] => United States
                [sort-name] => United States
                [iso-3166-1-code-list] => SimpleXMLElement Object
                    (
                        [iso-3166-1-code] => US
                    )

            )

    )

)

So I can output fields like name and country by

echo $response->artist->name;
echo $response->artist->country;

However I'm stuck when it comes to being able to access data in the attribute arrays. How can I get the type of group from the first attributes array for example?

Edit

I'm also trying to return the details from a function like so

func getDetails($id) {

$response = simplexml_load_file('http://musicbrainz.org/ws/2/artist/'.$id);
$data = array();
$data['type'] = $response->artist->attributes()->type;
$data['country'] = $response->artist->country;

return $data;
}

print_r(getDetails());

Gives me

MusicBrainz Object
(
)
1
  • $response->artist->attributes() will return an array of key/value pairs for the element attributes
    – Mark Baker
    Nov 9, 2013 at 18:51

1 Answer 1

1

You can access them using the attributes() method:

echo $response->artist->attributes()->type;

The example #5 in the SimpleXML documentation shows another example.

6
  • Thanks, that successfully outputs the value. However, I'm doing this inside of a function and trying to save the value to an array to return it. After returning the data I'm left with an empty object. Any ideas on what I can do here? Nov 9, 2013 at 19:46
  • @NickShears: How are you using this inside the function? Without seeing the relevant code, I can't help. :) Nov 9, 2013 at 19:48
  • Never mind sorted it, I needed to cast the values as strings when saving them to the array and I was getting an empty result due to assigning the result of the function to a variable with the same name of an object. Nov 9, 2013 at 20:24
  • With the id 489ce91b-6658-3307-9877-795b68554c98 i got as a result Array ( [type] => SimpleXMLElement Object ( [0] => Group ) [country] => SimpleXMLElement Object ( [0] => US ) ). You forgot to add the argument in print_r(getDetails());. Nov 9, 2013 at 20:25
  • i meant the id b9fb5447-7f95-4a6a-a157-afed2d7b9f4c ;) Nov 9, 2013 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.