我从XML文件中检索了数据
$response = simplexml_load_file($url);
print_r显示以下内容
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
)
)
)
)
所以我可以通过
输出名称和国家/地区等字段echo $response->artist->name;
echo $response->artist->country;
然而,当涉及到能够访问属性数组中的数据时,我会陷入困境。 我怎样才能从第一个属性数组中获取组的类型?
修改
我也试图从像这样的函数中返回细节
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());
给我
MusicBrainz Object
(
)
答案 0 :(得分:1)
您可以使用attributes()
方法访问它们:
echo $response->artist->attributes()->type;
SimpleXML文档中的example #5显示了另一个示例。