Jump to content

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;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.