Jump to content

XML Attribute Problem


johnslater

Recommended Posts

I've had this issue before and someone created a new function that did the job, however that function doesn't work in this case which is a pain.

 

I'm pulling an XML file and outputting the contents of the "album" tag and need to output each child element of album (name, playcount etc). I think the problem is with the album tag having a rank="1" attribute on it, so i can only output the first album, no further albums.

 

Here is what i have so far that outputs album one...

 

$artists_xml = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist='.$artist.'&api_key=b25b959554ed76058ac220b7b2e0a026');
$artists_array = $artists_xml->topalbums;
//print_r($artists_array);
foreach ($artists_array as $value)
{
	//VARS
	$album_title = $value->album->name;

	return $album_title;
}

 

I want to be able to loop through all of the albums, not just the first and be able to drill into each one and get the child elements.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/196768-xml-attribute-problem/
Share on other sites

You're not referencing the list of albums correctly. To get that right, use $artists_xml->topalbums->album and then change how you access items inside the loop to something like $value->name.

 

So to echo a list of album titles:

 

$albums_array = $artists_xml->topalbums->album;
foreach ($albums_array as $album)
{
$album_title = $album->name;
echo $album_title . PHP_EOL;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.