Reputation: 48
I've got a data structure in a variable $xml
that looks like this:
object(SimpleXMLElement)#3 (1) {
["release"]=>
object(SimpleXMLElement)#4 (11) {
["@attributes"]=>
array(1) {
["id"]=>
string(36) "49d996b2-ab53-41bd-8789-3d87938dc07d"
}
["title"]=>
string(9) "Pinkerton"
["status"]=>
string(8) "Official"
["packaging"]=>
string(10) "Jewel Case"
["quality"]=>
string(6) "normal"
["text-representation"]=>
object(SimpleXMLElement)#5 (2) {
["language"]=>
string(3) "eng"
["script"]=>
string(4) "Latn"
}
["artist-credit"]=>
object(SimpleXMLElement)#6 (1) {
["name-credit"]=>
object(SimpleXMLElement)#7 (1) {
["artist"]=>
object(SimpleXMLElement)#8 (3) {
["@attributes"]=>
array(1) {
["id"]=>
string(36) "6fe07aa5-fec0-4eca-a456-f29bff451b04"
}
["name"]=>
string(6) "Weezer"
["sort-name"]=>
string(6) "Weezer"
}
}
}
["date"]=>
string(10) "1996-09-24"
["country"]=>
string(2) "US"
["barcode"]=>
string(12) "720642500729"
["asin"]=>
string(10) "B000000OVP"
}
}
How would I reference the name 'Weezer' here? Or the sort name?
Here's what I've tried:
$a = (array)$xml->release['artist-credit']; // nothing
$a = $xml->release['artist-credit']; // nothing
$a = (array)$xml->release->artist-credit; // nothing
var_dump($a);
Upvotes: 0
Views: 154
Reputation: 31
Properties with hyphens need to be quoted in braces so something like this:
$a = $xml->release{'artist-credit'}
Will return the artist-credit portion of the xml. So to get the name:
$name = (string)$xml->release->{'artist-credit'}->{'name-credit'}->artist->name;
Note that it needs to be cast to a string, otherwise you'll still have a SimpleXMLElement object.
Upvotes: 1