I have this sample code from a lastfm script i found..:
function getArtistAlbums($artist, $size) {
$artist = urlencode($artist);
$xml = "http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist={$artist}&api_key=xxxxxxxxxxxxxxxxxxxxx";
$xml = @file_get_contents($xml);
if(!$xml) {
return; // Artist lookup failed.
}
$xml = new SimpleXMLElement($xml);
$xml = $xml->topalbums;
foreach ($xml->album as $album) {
$album_img = $album->image[$size];
$album_image = convert($album_img);
$album_name = $album->name;
//echo instead of returning
echo $album_name."<br>".$album_image."<br><br>";
}
but instead of echoing the results I want to return all $album_image
to a variable and call it from another file. I tried this:
$xml = new SimpleXMLElement($xml);
$xml = $xml->topalbums;
$values = array();
foreach ($xml->album as $album) {
$album_img = $album->image[$size];
$album_image = convert($album_img);
$album_name = $album->name;
$values[] = $album_image;
return $values;
//echo instead of returning
#echo $album_name."<br>".$album_image."<br><br>";
}
I know I need to build an array. I tried different things but I always get "Array" when I call the variable.
PS: i am very new with php so please bare with me, thanks in advance
The script is from here: http://techslides.com/lastfm-api-with-php/. In my other PHP script when I call the other functions, for example artist-album, I get a return with an image and then I can style it and output what I want. But I can't figure out how to output the above. When I call it inside an
echo '<div class='example'>'.getArtistAlbums(artist,2).'</div>';
it just echoes images.
The question is how can I somehow return variables and then use inside another variable as a did for example for artist-album. For example:
$artist_image = getArtist($artist,2);
and then use $artist_image
with echo
and clas
s etc.
echo
ing an array will showArray
. If you want to inspect the content of the array, you have to useprint_r
orvar_dump
. I don't see a point in returning inside the loop though. It will terminate the function after the first iteration. – Felix Kling Jun 7 at 23:01echo
the value instead of adding HTML to it). – Felix Kling Jun 7 at 23:24